summaryrefslogtreecommitdiff
path: root/source3/lib/talloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'source3/lib/talloc.c')
-rw-r--r--source3/lib/talloc.c84
1 files changed, 73 insertions, 11 deletions
diff --git a/source3/lib/talloc.c b/source3/lib/talloc.c
index 093a221fd3..cafe065479 100644
--- a/source3/lib/talloc.c
+++ b/source3/lib/talloc.c
@@ -54,6 +54,8 @@
#include "includes.h"
+/* Max allowable allococation - 256mb - 0x10000000 */
+#define MAX_TALLOC_SIZE (1024*1024*256)
/**
* Start of linked list of all talloc pools.
@@ -100,7 +102,7 @@ static TALLOC_CTX *talloc_init_internal(void)
{
TALLOC_CTX *t;
- t = (TALLOC_CTX *)malloc(sizeof(TALLOC_CTX));
+ t = (TALLOC_CTX *)SMB_MALLOC(sizeof(TALLOC_CTX));
if (t) {
t->list = NULL;
t->total_alloc_size = 0;
@@ -143,16 +145,20 @@ static TALLOC_CTX *talloc_init_internal(void)
/** Allocate a bit of memory from the specified pool **/
+#if defined(PARANOID_MALLOC_CHECKER)
+void *talloc_(TALLOC_CTX *t, size_t size)
+#else
void *talloc(TALLOC_CTX *t, size_t size)
+#endif
{
void *p;
struct talloc_chunk *tc;
if (!t || size == 0) return NULL;
- p = malloc(size);
+ p = SMB_MALLOC(size);
if (p) {
- tc = malloc(sizeof(*tc));
+ tc = SMB_MALLOC(sizeof(*tc));
if (tc) {
tc->ptr = p;
tc->size = size;
@@ -167,8 +173,25 @@ void *talloc(TALLOC_CTX *t, size_t size)
return p;
}
+/** Allocate an array of count elements of size x */
+#if defined(PARANOID_MALLOC_CHECKER)
+void *talloc_array_(TALLOC_CTX *ctx, size_t el_size, unsigned int count)
+#else
+void *talloc_array(TALLOC_CTX *ctx, size_t el_size, unsigned int count)
+#endif
+{
+ if (count >= MAX_TALLOC_SIZE/el_size) {
+ return NULL;
+ }
+ return TALLOC(ctx, el_size * count);
+}
+
/** A talloc version of realloc */
+#if defined(PARANOID_MALLOC_CHECKER)
+void *talloc_realloc_(TALLOC_CTX *t, void *ptr, size_t size)
+#else
void *talloc_realloc(TALLOC_CTX *t, void *ptr, size_t size)
+#endif
{
struct talloc_chunk *tc;
void *new_ptr;
@@ -179,11 +202,11 @@ void *talloc_realloc(TALLOC_CTX *t, void *ptr, size_t size)
/* realloc(NULL) is equavalent to malloc() */
if (ptr == NULL)
- return talloc(t, size);
+ return TALLOC(t, size);
for (tc=t->list; tc; tc=tc->next) {
if (tc->ptr == ptr) {
- new_ptr = Realloc(ptr, size);
+ new_ptr = SMB_REALLOC(ptr, size);
if (new_ptr) {
t->total_alloc_size += (size - tc->size);
tc->size = size;
@@ -195,6 +218,19 @@ void *talloc_realloc(TALLOC_CTX *t, void *ptr, size_t size)
return NULL;
}
+/** Re-allocate an array of count elements of size x */
+#if defined(PARANOID_MALLOC_CHECKER)
+void *talloc_realloc_array_(TALLOC_CTX *ctx, void *ptr, size_t el_size, unsigned int count)
+#else
+void *talloc_realloc_array(TALLOC_CTX *ctx, void *ptr, size_t el_size, unsigned int count)
+#endif
+{
+ if (count >= MAX_TALLOC_SIZE/el_size) {
+ return NULL;
+ }
+ return TALLOC_REALLOC(ctx, ptr, el_size * count);
+}
+
/** Destroy all the memory allocated inside @p t, but not @p t
* itself. */
void talloc_destroy_pool(TALLOC_CTX *t)
@@ -246,9 +282,13 @@ const char * talloc_pool_name(TALLOC_CTX const *t)
/** talloc and zero memory. */
+#if defined(PARANOID_MALLOC_CHECKER)
+void *talloc_zero_(TALLOC_CTX *t, size_t size)
+#else
void *talloc_zero(TALLOC_CTX *t, size_t size)
+#endif
{
- void *p = talloc(t, size);
+ void *p = TALLOC(t, size);
if (p)
memset(p, '\0', size);
@@ -256,10 +296,32 @@ void *talloc_zero(TALLOC_CTX *t, size_t size)
return p;
}
+#if defined(PARANOID_MALLOC_CHECKER)
+void *talloc_zero_array_(TALLOC_CTX *t, size_t el_size, unsigned int count)
+#else
+void *talloc_zero_array(TALLOC_CTX *t, size_t el_size, unsigned int count)
+#endif
+{
+#if defined(PARANOID_MALLOC_CHECKER)
+ void *p = talloc_array_(t, el_size, count);
+#else
+ void *p = talloc_array(t, el_size, count);
+#endif
+
+ if (p)
+ memset(p, '\0', el_size*count);
+
+ return p;
+}
+
/** memdup with a talloc. */
+#if defined(PARANOID_MALLOC_CHECKER)
+void *talloc_memdup_(TALLOC_CTX *t, const void *p, size_t size)
+#else
void *talloc_memdup(TALLOC_CTX *t, const void *p, size_t size)
+#endif
{
- void *newp = talloc(t,size);
+ void *newp = TALLOC(t,size);
if (newp)
memcpy(newp, p, size);
@@ -271,7 +333,7 @@ void *talloc_memdup(TALLOC_CTX *t, const void *p, size_t size)
char *talloc_strdup(TALLOC_CTX *t, const char *p)
{
if (p)
- return talloc_memdup(t, p, strlen(p) + 1);
+ return TALLOC_MEMDUP(t, p, strlen(p) + 1);
else
return NULL;
}
@@ -298,7 +360,7 @@ char *talloc_strdup_upper(TALLOC_CTX *t, const char *p)
smb_ucs2_t *talloc_strdup_w(TALLOC_CTX *t, const smb_ucs2_t *p)
{
if (p)
- return talloc_memdup(t, p, (strlen_w(p) + 1) * sizeof(smb_ucs2_t));
+ return TALLOC_MEMDUP(t, p, (strlen_w(p) + 1) * sizeof(smb_ucs2_t));
else
return NULL;
}
@@ -329,7 +391,7 @@ smb_ucs2_t *talloc_strdup_w(TALLOC_CTX *t, const smb_ucs2_t *p)
len = vsnprintf(NULL, 0, fmt, ap2);
- ret = talloc(t, len+1);
+ ret = TALLOC(t, len+1);
if (ret) {
VA_COPY(ap2, ap);
vsnprintf(ret, len+1, fmt, ap2);
@@ -373,7 +435,7 @@ smb_ucs2_t *talloc_strdup_w(TALLOC_CTX *t, const smb_ucs2_t *p)
s_len = strlen(s);
len = vsnprintf(NULL, 0, fmt, ap2);
- s = talloc_realloc(t, s, s_len + len+1);
+ s = TALLOC_REALLOC(t, s, s_len + len+1);
if (!s) return NULL;
VA_COPY(ap2, ap);