1 |
/* PEAK Library |
2 |
* |
3 |
* Copyright (c) 2003-2005 |
4 |
* Stephane Thiell <mbuna@bugged.org>. All rights reserved. |
5 |
* |
6 |
* Redistribution and use in source and binary forms, with or without |
7 |
* modification, are permitted provided that the following conditions |
8 |
* are met: |
9 |
* |
10 |
* 1. Redistributions of source code must retain the above copyright |
11 |
* notice, this list of conditions and the following disclaimer. |
12 |
* |
13 |
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
* notice, this list of conditions and the following disclaimer in the |
15 |
* documentation and/or other materials provided with the distribution. |
16 |
* |
17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
19 |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
24 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
25 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
26 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
27 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 |
* |
29 |
*/ |
30 |
#define RCSID "$Id: alloc.c,v 1.2 2005/01/27 16:31:49 mbuna Exp $" |
31 |
|
32 |
#ifdef HAVE_CONFIG_H |
33 |
#include "config.h" |
34 |
#endif |
35 |
|
36 |
#include <peak/alloc.h> |
37 |
#include "internal.h" |
38 |
|
39 |
#include <assert.h> |
40 |
#include <stdlib.h> |
41 |
#include <string.h> |
42 |
|
43 |
static peak_alloc_malloc_func __malloc_fun = malloc; |
44 |
static peak_alloc_free_func __free_fun = free; |
45 |
|
46 |
void |
47 |
peak_alloc_configure(peak_alloc_malloc_func malloc_fun, |
48 |
peak_alloc_free_func free_fun) |
49 |
{ |
50 |
__malloc_fun = (malloc_fun) ? malloc_fun : malloc; |
51 |
__free_fun = (free_fun) ? free_fun : free; |
52 |
} |
53 |
|
54 |
void* |
55 |
peak_allocate(size_t size) |
56 |
{ |
57 |
void *ptr = (*__malloc_fun)(size); |
58 |
if (ptr == NULL) |
59 |
PEAK_HALT; |
60 |
return ptr; |
61 |
} |
62 |
|
63 |
void |
64 |
peak_deallocate(void *ptr) |
65 |
{ |
66 |
(*__free_fun)(ptr); |
67 |
} |
68 |
|
69 |
char * |
70 |
peak_strdup(const char *str) |
71 |
{ |
72 |
size_t len = strlen(str) + 1; |
73 |
return memcpy(peak_allocate(len), str, len); |
74 |
} |
75 |
|
76 |
/* peak_retain and peak_release are defined in runtime.c |
77 |
*/ |