summaryrefslogtreecommitdiffstats
path: root/util/src/allocator.c
blob: 40261a9632b4333bb1f77f9a8eab5089c048c2a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <axutil_allocator.h>
#include <axutil_utils.h>
#include <stdlib.h>
#include <string.h>

void *AXIS2_CALL axutil_allocator_malloc_impl(
    axutil_allocator_t * allocator,
    size_t size);

void *AXIS2_CALL axutil_allocator_realloc_impl(
    axutil_allocator_t * allocator,
    void *ptr,
    size_t size);

void AXIS2_CALL axutil_allocator_free_impl(
    axutil_allocator_t * allocator,
    void *ptr);

AXIS2_EXTERN axutil_allocator_t *AXIS2_CALL
axutil_allocator_init(
    axutil_allocator_t * allocator)
{
    if(!allocator)
    {
        allocator = (axutil_allocator_t *)malloc(sizeof(axutil_allocator_t));
        if (allocator)
        {
            memset(allocator, 0, sizeof(axutil_allocator_t));
            allocator->malloc_fn = axutil_allocator_malloc_impl;
            allocator->realloc = axutil_allocator_realloc_impl;
            allocator->free_fn = axutil_allocator_free_impl;
            allocator->global_pool_ref = 0;
        }
    }

    return allocator;
}

AXIS2_EXTERN axutil_allocator_t *AXIS2_CALL
axutil_allocator_clone(
    axutil_allocator_t * allocator)
{
    if(!allocator)
        return NULL;

    else
    {
        axutil_allocator_t* clone = NULL;
        clone = (axutil_allocator_t *)malloc(sizeof(axutil_allocator_t));
        memset(clone, 0, sizeof(axutil_allocator_t));
        if(clone)
        {
            clone->malloc_fn = allocator->malloc_fn;
            clone->realloc = allocator->realloc;
            clone->free_fn = allocator->free_fn;
            clone->current_pool = allocator->current_pool;
            clone->global_pool = allocator->global_pool;
            clone->local_pool = allocator->local_pool;
            clone->global_pool_ref = 0;

            return clone;
        }
    }
    return NULL;
}
AXIS2_EXTERN void AXIS2_CALL
axutil_allocator_free(
    axutil_allocator_t * allocator)
{
    if(allocator)
    {
        allocator->free_fn(allocator, allocator);
    }
    return;
}

void *AXIS2_CALL
axutil_allocator_malloc_impl(
    axutil_allocator_t * allocator,
    size_t size)
{
    return malloc(size);
}

void *AXIS2_CALL
axutil_allocator_realloc_impl(
    axutil_allocator_t * allocator,
    void *ptr,
    size_t size)
{
    return realloc(ptr, size);
}

void AXIS2_CALL
axutil_allocator_free_impl(
    axutil_allocator_t * allocator,
    void *ptr)
{
    free(ptr);
}

AXIS2_EXTERN void AXIS2_CALL
axutil_allocator_switch_to_global_pool(
    axutil_allocator_t * allocator)
{
    if(!allocator)
        return;
    allocator->global_pool_ref++;
    allocator->current_pool = allocator->global_pool;
    return;
}

AXIS2_EXTERN void AXIS2_CALL
axutil_allocator_switch_to_local_pool(
    axutil_allocator_t * allocator)
{
    if(!allocator)
        return;
    if(allocator->global_pool_ref > 0)
    {
        allocator->global_pool_ref--;
    }

    if(allocator->global_pool_ref == 0)
    {
        allocator->current_pool = allocator->local_pool;
    }
    return;
}