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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
/*
ldb database library
Copyright (C) Andrew Tridgell 2004
** NOTE! The following LGPL license applies to the ldb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Name: ldb
*
* Component: ldb alloc
*
* Description: functions for memory allocation
*
* Author: Andrew Tridgell
*/
#include "includes.h"
/*
this allows the user to choose their own allocation function
*/
int ldb_set_alloc(struct ldb_context *ldb,
void *(*alloc)(void *context, void *ptr, size_t size),
void *context)
{
ldb->alloc_ops.alloc = alloc;
ldb->alloc_ops.context = context;
return 0;
}
/*
this is the default memory allocation function
*/
static void *ldb_default_alloc(void *context, void *ptr, size_t size)
{
/* by setting LDB_ALLOC_OFS to non-zero the test suite can
catch any places where we incorrectly use the libc alloc
funcitons directly */
#define LDB_ALLOC_OFS 4
/* we don't assume a modern realloc function */
if (ptr == NULL) {
ptr = malloc(size+LDB_ALLOC_OFS);
if (ptr) return ((char *)ptr)+LDB_ALLOC_OFS;
return NULL;
}
if (size == 0) {
free(((char *)ptr)-LDB_ALLOC_OFS);
return NULL;
}
ptr = realloc(((char *)ptr)-LDB_ALLOC_OFS, size+LDB_ALLOC_OFS);
if (ptr) {
return ((char *)ptr)+LDB_ALLOC_OFS;
}
return NULL;
}
/*
all memory allocation goes via this function
*/
void *ldb_realloc(struct ldb_context *ldb, void *ptr, size_t size)
{
if (!ldb->alloc_ops.alloc) {
ldb_set_alloc(ldb, ldb_default_alloc, NULL);
}
return ldb->alloc_ops.alloc(ldb->alloc_ops.context, ptr, size);
}
void *ldb_malloc(struct ldb_context *ldb, size_t size)
{
return ldb_realloc(ldb, NULL, size);
}
void ldb_free(struct ldb_context *ldb, void *ptr)
{
if (ptr != NULL) {
ldb_realloc(ldb, ptr, 0);
}
}
void *ldb_strndup(struct ldb_context *ldb, const char *str, size_t maxlen)
{
size_t len = strnlen(str, maxlen);
void *ret;
ret = ldb_realloc(ldb, NULL, len+1);
if (ret) {
memcpy(ret, str, len);
((char *)ret)[len] = 0;
}
return ret;
}
void *ldb_strdup(struct ldb_context *ldb, const char *str)
{
size_t len = strlen(str);
void *ret;
ret = ldb_realloc(ldb, NULL, len+1);
if (ret) {
memcpy(ret, str, len+1);
}
return ret;
}
/*
a ldb wrapper for asprintf(), using ldb_malloc()
*/
int ldb_asprintf(struct ldb_context *ldb, char **strp, const char *fmt, ...)
{
int len, len2;
va_list ap;
*strp = NULL;
va_start(ap, fmt);
len = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
if (len < 0) {
return len;
}
*strp = ldb_malloc(ldb, len+1);
if (! *strp) {
return -1;
}
va_start(ap, fmt);
len2 = vsnprintf(*strp, len+1, fmt, ap);
va_end(ap);
if (len2 != len) {
/* buggy (or non-C99) vsnprintf function */
ldb_free(ldb, *strp);
return -1;
}
return len;
}
/*
realloc an array, checking for integer overflow in the array size
*/
void *ldb_realloc_array(struct ldb_context *ldb,
void *ptr, size_t el_size, unsigned count)
{
#define MAX_MALLOC_SIZE 0x7fffffff
if (count == 0 ||
count >= MAX_MALLOC_SIZE/el_size) {
return NULL;
}
if (!ptr) {
return ldb_malloc(ldb, el_size * count);
}
return ldb_realloc(ldb, ptr, el_size * count);
}
|