1 | /******************************************************************* |
---|
2 | * File: omalloc.c |
---|
3 | * Purpose: implementation of ANSI-C conforming malloc functions |
---|
4 | * -- the real version |
---|
5 | * Author: obachman@mathematik.uni-kl.de (Olaf Bachmann) |
---|
6 | * Created: 11/99 |
---|
7 | *******************************************************************/ |
---|
8 | |
---|
9 | #include <stdlib.h> |
---|
10 | #include <stdio.h> |
---|
11 | |
---|
12 | #ifndef OMALLOC_C |
---|
13 | #define OMALLOC_C |
---|
14 | |
---|
15 | #include <omalloc/omalloc.h> |
---|
16 | |
---|
17 | #ifdef OM_MALLOC_MARK_AS_STATIC |
---|
18 | #define OM_MARK_AS_STATIC(addr) omMarkAsStaticAddr(addr) |
---|
19 | #else |
---|
20 | #define OM_MARK_AS_STATIC(addr) do {} while (0) |
---|
21 | #endif |
---|
22 | |
---|
23 | #if OM_PROVIDE_MALLOC > 0 |
---|
24 | |
---|
25 | void* calloc(size_t nmemb, size_t size) |
---|
26 | { |
---|
27 | void* addr; |
---|
28 | if (size == 0) size = 1; |
---|
29 | if (nmemb == 0) nmemb = 1; |
---|
30 | |
---|
31 | size = size*nmemb; |
---|
32 | omTypeAlloc0Aligned(void*, addr, size); |
---|
33 | OM_MARK_AS_STATIC(addr); |
---|
34 | return addr; |
---|
35 | } |
---|
36 | |
---|
37 | void free(void* addr) |
---|
38 | { |
---|
39 | omfree(addr); |
---|
40 | } |
---|
41 | |
---|
42 | void* valloc(size_t size) |
---|
43 | { |
---|
44 | fprintf(stderr, "omalloc Warning: valloc not yet implemented\n"); |
---|
45 | fflush(NULL); |
---|
46 | return NULL; |
---|
47 | } |
---|
48 | |
---|
49 | #if defined(sgi) |
---|
50 | void* memalign(size_t size_1, size_t size_2) |
---|
51 | #elif (defined(__sun) && (defined(__sparc) || defined(__i386) || defined(__x86_64)) || defined(__CYGWIN__)) |
---|
52 | void* memalign(size_t size_1, size_t size_2) |
---|
53 | #else |
---|
54 | void* memalign(void* alignment, size_t size) |
---|
55 | #endif |
---|
56 | { |
---|
57 | fprintf(stderr, "omalloc Warning: memalign not yet implemented\n"); |
---|
58 | fflush(NULL); |
---|
59 | return NULL; |
---|
60 | } |
---|
61 | |
---|
62 | void* realloc(void* old_addr, size_t new_size) |
---|
63 | { |
---|
64 | if (old_addr && new_size) |
---|
65 | { |
---|
66 | void* new_addr; |
---|
67 | omTypeReallocAligned(old_addr, void*, new_addr, new_size); |
---|
68 | OM_MARK_AS_STATIC(new_addr); |
---|
69 | return new_addr; |
---|
70 | } |
---|
71 | else |
---|
72 | { |
---|
73 | free(old_addr); |
---|
74 | return malloc(new_size); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | /* on some systems strdup is a macro -- replace it unless OMALLOC_FUNC |
---|
79 | is defined */ |
---|
80 | #ifndef OMALLOC_USES_MALLOC |
---|
81 | #if !defined(OMALLOC_FUNC) |
---|
82 | #undef strdup |
---|
83 | #endif |
---|
84 | char* strdup_(const char* addr) |
---|
85 | { |
---|
86 | char* n_s; |
---|
87 | if (addr) |
---|
88 | { |
---|
89 | n_s = omStrDup(addr); |
---|
90 | OM_MARK_AS_STATIC(n_s); |
---|
91 | return n_s; |
---|
92 | } |
---|
93 | return NULL; |
---|
94 | } |
---|
95 | #endif |
---|
96 | #endif |
---|
97 | |
---|
98 | void* malloc(size_t size) |
---|
99 | { |
---|
100 | void* addr; |
---|
101 | if (size == 0) size = 1; |
---|
102 | |
---|
103 | omTypeAllocAligned(void*, addr, size); |
---|
104 | OM_MARK_AS_STATIC(addr); |
---|
105 | return addr; |
---|
106 | } |
---|
107 | |
---|
108 | void freeSize(void* addr, size_t size) |
---|
109 | { |
---|
110 | if (addr) omFreeSize(addr, size); |
---|
111 | } |
---|
112 | |
---|
113 | void* reallocSize(void* old_addr, size_t old_size, size_t new_size) |
---|
114 | { |
---|
115 | if (old_addr && new_size) |
---|
116 | { |
---|
117 | void* new_addr; |
---|
118 | omTypeReallocAlignedSize(old_addr, old_size, void*, new_addr, new_size); |
---|
119 | OM_MARK_AS_STATIC(new_addr); |
---|
120 | return new_addr; |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | freeSize(old_addr, old_size); |
---|
125 | return malloc(new_size); |
---|
126 | } |
---|
127 | } |
---|
128 | #endif |
---|