int main(int argc, char* argv[]) { int *a = (int*) l_malloc(sizeof(int)); l_free(a); l_free(a); return 0; }
char *l_mprintf(const char *fmt, ...) { va_list args, argscpy; va_start(args, fmt); va_copy(argscpy, args); size_t needed = vsnprintf(NULL, 0, fmt, argscpy); char *buf = l_malloc(needed+1); va_end(argscpy); vsprintf(buf, fmt, args); va_end(args); return buf; }
static void *l_malloc_array(int nxmin, int nxmax, size_t el_size) { /* Allocates an array with nxmax - nxmin + 1 elements, with each element * of size el_size. i.e. returns a pointer to a storage block [nxmin..nxmax]. * Simply cast the returned array pointer to the proper type. */ char *cptr; cptr = (char *)l_malloc((nxmax - nxmin + 1) * el_size); cptr -= nxmin * el_size / sizeof(char); return (void *)cptr; }
static l_random_context_t * l_random_get_context(void) { l_random_context_t *ctx = pthread_getspecific(key); if (!ctx) { ctx = l_malloc(sizeof(l_random_context_t)); ctx->fd = open("/dev/urandom", O_RDONLY); if (ctx->fd < 0) { l_log_errno(L_CRIT, errno, "Failed opening random source: open failed()"); l_die(); } pthread_setspecific(key, ctx); } return ctx; }
l_column_result_id_t l_column_results_put(l_column_results_t *results, l_column_result_t *result) { l_column_result_t *result_cpy = l_malloc(sizeof(l_column_result_t)); memcpy(result_cpy, result, sizeof(l_column_result_t)); int res = pthread_rwlock_wrlock(&results->lock); if (res) { l_log_errno(L_ERR, res, "pthread_rwlock_wrlock() failed"); return 0; } l_column_result_id_t id = results->next_id++; l_map_add(&results->result_map, &id, sizeof(l_column_result_id_t), result_cpy); res = pthread_rwlock_unlock(&results->lock); if (res) { l_log_errno(L_ERR, res, "pthread_rwlock_unlock() failed"); return 0; } return id; }