gc_malloc (size_t size, bool clear, struct gc_arena *a) #endif { void *ret; if (a) { struct gc_entry *e; #ifdef DMALLOC e = (struct gc_entry *) openvpn_dmalloc (file, line, size + sizeof (struct gc_entry)); #else e = (struct gc_entry *) malloc (size + sizeof (struct gc_entry)); #endif check_malloc_return (e); ret = (char *) e + sizeof (struct gc_entry); e->next = a->list; a->list = e; } else { #ifdef DMALLOC ret = openvpn_dmalloc (file, line, size); #else ret = malloc (size); #endif check_malloc_return (ret); } #ifndef ZERO_BUFFER_ON_ALLOC if (clear) #endif memset (ret, 0, size); return ret; }
string_alloc (const char *str, struct gc_arena *gc) #endif { if (str) { const int n = strlen (str) + 1; char *ret; if (gc) { #ifdef DMALLOC ret = (char *) gc_malloc_debug (n, false, gc, file, line); #else ret = (char *) gc_malloc (n, false, gc); #endif } else { /* If there are no garbage collector available, it's expected * that the caller cleans up afterwards. This is coherent with the * earlier behaviour when gc_malloc() would be called with gc == NULL */ #ifdef DMALLOC ret = openvpn_dmalloc (file, line, n); memset(ret, 0, n); #else ret = calloc(1, n); #endif check_malloc_return(ret); } memcpy (ret, str, n); return ret; } else return NULL; }
clone_buf (const struct buffer* buf) #endif { struct buffer ret; ret.capacity = buf->capacity; ret.offset = buf->offset; ret.len = buf->len; #ifdef DMALLOC ret.data = (uint8_t *) openvpn_dmalloc (file, line, buf->capacity); #else ret.data = (uint8_t *) malloc (buf->capacity); #endif check_malloc_return (ret.data); memcpy (BPTR (&ret), BPTR (buf), BLEN (buf)); return ret; }
void gc_addspecial (void *addr, void (free_function)(void*), struct gc_arena *a) { ASSERT(a); struct gc_entry_special *e; #ifdef DMALLOC e = (struct gc_entry_special *) openvpn_dmalloc (file, line, sizeof (struct gc_entry_special)); #else e = (struct gc_entry_special *) malloc (sizeof (struct gc_entry_special)); #endif check_malloc_return (e); e->free_fnc = free_function; e->addr = addr; e->next = a->list_special; a->list_special = e; }
/* * This is exactly like clone_buf, but uses malloc to allocate the memory so that it can be cached * properly */ struct buffer* full_clone_buf(const struct buffer* buf) { struct buffer* ret = (struct buffer*) malloc(sizeof(struct buffer)); ret->capacity = buf->capacity; ret->offset = buf->offset; ret->len = buf->len; #ifdef DMALLOC ret->data = (uint8_t *) openvpn_dmalloc (file, line, buf->capacity); #else ret->data = (uint8_t *) malloc (buf->capacity); #endif check_malloc_return (ret->data); memcpy (BPTR (ret), BPTR (buf), BLEN (buf)); return ret; }
alloc_buf (size_t size) #endif { struct buffer buf; if (!buf_size_valid (size)) buf_size_error (size); buf.capacity = (int)size; buf.offset = 0; buf.len = 0; #ifdef DMALLOC buf.data = openvpn_dmalloc (file, line, size); #else buf.data = calloc (1, size); #endif check_malloc_return(buf.data); return buf; }