gsoap源码分析_alist表

[TOC]


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
typedef void* ALIST_T

#define SOAP_MAXALLOCSIZE (0)

#define SOAP_CANARY (0xC0DE)

void *(ALIST_T *list, size_t n);
void *soap_calloc(ALIST_T *list, size_t n);
char *soap_strdup(ALIST_T *list, const char *s);

void soap_echo_alist(ALIST_T *list);

void soap_dealloc(ALIST_T *list, void *p);

源码
void* (ALIST_T *list, size_t n)
{
char *p;

if (SOAP_MAXALLOCSIZE > 0 && n > SOAP_MAXALLOCSIZE)
return NULL;
if (n + sizeof(short) < n)
return NULL;

n += sizeof(short);
if (n + ((-(long)n) & (sizeof(void*)-1)) + sizeof(void*) + sizeof(size_t) < n)
return NULL;
n += (-(long)n) & (sizeof(void*)-1); /* align at 4-, 8- or 16-byte boundary by rounding up */
p = (char*)SOAP_MALLOC(n + sizeof(void*) + sizeof(size_t));
if (!p)
{
return NULL;
}
/* set a canary word to detect memory overruns and data corruption */
*(unsigned short*)(p + n - sizeof(unsigned short)) = (unsigned short)SOAP_CANARY;
/* keep chain of alloced cells for destruction */
*(void**)(p + n) = *list;
*(size_t*)(p + n + sizeof(void*)) = n;
*list = p + n;
PRINT_Y("[%d]List:%x", n, *list);

return p;
}
void* soap_calloc(ALIST_T *list, size_t n)
{
char *p;

p = soap_malloc(list, n);
if (p)
{
memset(p, 0, n);
}

return p;
}

char *soap_strdup(ALIST_T *list, const char *s)
{
char *t = NULL;
if (s)
{
size_t n = strlen(s) + 1;
if (n > 0)
{ t = (char*)soap_malloc(list, n);
if (t)
{ memcpy((void*)t, (const void*)s, n);
t[n - 1] = '