1 /*
2 * allocator.c
3 *
4 * lispreader
5 *
6 * Copyright (C) 2004 Mark Probst
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23
24 #include <allocator.h>
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 static void*
malloc_allocator_alloc(void * allocator_data,size_t size)30 malloc_allocator_alloc (void *allocator_data, size_t size)
31 {
32 return malloc(size);
33 }
34
35 static void
malloc_allocator_free(void * allocator_data,void * chunk)36 malloc_allocator_free (void *allocator_data, void *chunk)
37 {
38 free(chunk);
39 }
40
41 allocator_t malloc_allocator = { malloc_allocator_alloc, malloc_allocator_free, 0 };
42
43 static void
pools_allocator_free(void * allocator_data,void * chunk)44 pools_allocator_free (void *allocator_data, void *chunk)
45 {
46 }
47
48 void
init_pools_allocator(allocator_t * allocator,pools_t * pools)49 init_pools_allocator (allocator_t *allocator, pools_t *pools)
50 {
51 allocator->alloc = (void* (*) (void*, size_t))pools_alloc;
52 allocator->free = pools_allocator_free;
53 allocator->allocator_data = pools;
54 }
55
56 char*
allocator_strdup(allocator_t * allocator,const char * str)57 allocator_strdup (allocator_t *allocator, const char *str)
58 {
59 size_t len = strlen(str) + 1;
60 char *copy = (char*)allocator_alloc(allocator, len);
61
62 if (copy != 0)
63 memcpy(copy, str, len);
64
65 return copy;
66 }
67