File size: 2,578 Bytes
fd49381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "xmlrpc_config.h"

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "bool.h"
#include "girmath.h"
#include "mallocvar.h"
#include "xmlrpc-c/util_int.h"
#include "xmlrpc-c/util.h"

struct _xmlrpc_mem_pool {
    size_t       size;
    size_t       allocated;
};



xmlrpc_mem_pool * 
xmlrpc_mem_pool_new(xmlrpc_env * const envP, 
                    size_t       const size) {
/*----------------------------------------------------------------------------
   Create an xmlrpc_mem_pool of size 'size' bytes.
-----------------------------------------------------------------------------*/
    xmlrpc_mem_pool * poolP;

    XMLRPC_ASSERT_ENV_OK(envP);

    MALLOCVAR(poolP);
    
    if (poolP == NULL)
        xmlrpc_faultf(envP, "Can't allocate memory pool descriptor");
    else {
        poolP->size = size;

        poolP->allocated = 0;
    
        if (envP->fault_occurred)
            free(poolP);
    }
    return poolP;
}



void
xmlrpc_mem_pool_free(xmlrpc_mem_pool * const poolP) {
/*----------------------------------------------------------------------------
   Destroy xmlrpc_mem_pool *poolP.
-----------------------------------------------------------------------------*/
    XMLRPC_ASSERT(poolP != NULL);

    XMLRPC_ASSERT(poolP->allocated == 0);

    free(poolP);
}



void
xmlrpc_mem_pool_alloc(xmlrpc_env *      const envP, 
                      xmlrpc_mem_pool * const poolP,
                      size_t            const size) {
/*----------------------------------------------------------------------------
   Take 'size' bytes from pool *poolP.
-----------------------------------------------------------------------------*/
    XMLRPC_ASSERT(poolP->allocated <= poolP->size);

    if (poolP->size - poolP->allocated < size)
        xmlrpc_env_set_fault_formatted(
            envP, XMLRPC_LIMIT_EXCEEDED_ERROR,
            "Memory pool is out of memory.  %u-byte pool is %u bytes short",
            (unsigned)poolP->size,
            (unsigned)(poolP->allocated + size - poolP->size));
    else
        poolP->allocated += size;
}


                      
void
xmlrpc_mem_pool_release(xmlrpc_mem_pool * const poolP,
                        size_t            const size) {
/*----------------------------------------------------------------------------
   Return 'size' bytes to pool *poolP.
-----------------------------------------------------------------------------*/
    XMLRPC_ASSERT(poolP != NULL);

    XMLRPC_ASSERT(poolP->allocated >= size);

    poolP->allocated -= size;
}