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) ((void)0) |
---|
21 | #endif |
---|
22 | |
---|
23 | void* calloc(size_t nmemb, size_t size) |
---|
24 | { |
---|
25 | void* addr; |
---|
26 | if (size == 0) size = 1; |
---|
27 | if (nmemb == 0) nmemb = 1; |
---|
28 | |
---|
29 | size = size*nmemb; |
---|
30 | omTypeAlloc0Aligned(void*, addr, size); |
---|
31 | OM_MARK_AS_STATIC(addr); |
---|
32 | return addr; |
---|
33 | } |
---|
34 | |
---|
35 | void* malloc(size_t size) |
---|
36 | { |
---|
37 | void* addr; |
---|
38 | if (size == 0) size = 1; |
---|
39 | |
---|
40 | omTypeAllocAligned(void*, addr, size); |
---|
41 | OM_MARK_AS_STATIC(addr); |
---|
42 | return addr; |
---|
43 | } |
---|
44 | |
---|
45 | void free(void* addr) |
---|
46 | { |
---|
47 | omfree(addr); |
---|
48 | } |
---|
49 | |
---|
50 | void* valloc(size_t size) |
---|
51 | { |
---|
52 | fprintf(stderr, "omalloc Warning: valloc not yet implemented\n"); |
---|
53 | fflush(NULL); |
---|
54 | return NULL; |
---|
55 | } |
---|
56 | |
---|
57 | #if defined(sgi) |
---|
58 | void* memalign(size_t size_1, size_t size_2) |
---|
59 | #elif (defined(__sun) && (defined(__sparc) || defined(__i386) || defined(__x86_64)) || defined(__CYGWIN__)) |
---|
60 | void* memalign(size_t size_1, size_t size_2) |
---|
61 | #else |
---|
62 | void* memalign(void* alignment, size_t size) |
---|
63 | #endif |
---|
64 | { |
---|
65 | fprintf(stderr, "omalloc Warning: memalign not yet implemented\n"); |
---|
66 | fflush(NULL); |
---|
67 | return NULL; |
---|
68 | } |
---|
69 | |
---|
70 | void* realloc(void* old_addr, size_t new_size) |
---|
71 | { |
---|
72 | if (old_addr && new_size) |
---|
73 | { |
---|
74 | void* new_addr; |
---|
75 | omTypeReallocAligned(old_addr, void*, new_addr, new_size); |
---|
76 | OM_MARK_AS_STATIC(new_addr); |
---|
77 | return new_addr; |
---|
78 | } |
---|
79 | else |
---|
80 | { |
---|
81 | free(old_addr); |
---|
82 | return malloc(new_size); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | /* on some systems strdup is a macro -- replace it unless OMALLOC_FUNC |
---|
87 | is defined */ |
---|
88 | #ifndef OMALLOC_USES_MALLOC |
---|
89 | #if !defined(OMALLOC_FUNC) |
---|
90 | #undef strdup |
---|
91 | #endif |
---|
92 | char* strdup_(const char* addr) |
---|
93 | { |
---|
94 | char* n_s; |
---|
95 | if (addr) |
---|
96 | { |
---|
97 | n_s = omStrDup(addr); |
---|
98 | OM_MARK_AS_STATIC(n_s); |
---|
99 | return n_s; |
---|
100 | } |
---|
101 | return NULL; |
---|
102 | } |
---|
103 | #endif |
---|
104 | void freeSize(void* addr, size_t size) |
---|
105 | { |
---|
106 | if (addr) omFreeSize(addr, size); |
---|
107 | } |
---|
108 | |
---|
109 | void* reallocSize(void* old_addr, size_t old_size, size_t new_size) |
---|
110 | { |
---|
111 | if (old_addr && new_size) |
---|
112 | { |
---|
113 | void* new_addr; |
---|
114 | omTypeReallocAlignedSize(old_addr, old_size, void*, new_addr, new_size); |
---|
115 | OM_MARK_AS_STATIC(new_addr); |
---|
116 | return new_addr; |
---|
117 | } |
---|
118 | else |
---|
119 | { |
---|
120 | freeSize(old_addr, old_size); |
---|
121 | return malloc(new_size); |
---|
122 | } |
---|
123 | } |
---|
124 | #endif |
---|