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