[e70e45] | 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 | |
---|
[0f401f] | 15 | #include <omalloc/omalloc.h> |
---|
[e70e45] | 16 | |
---|
| 17 | #ifdef OM_MALLOC_MARK_AS_STATIC |
---|
| 18 | #define OM_MARK_AS_STATIC(addr) omMarkAsStaticAddr(addr) |
---|
| 19 | #else |
---|
[d101b1] | 20 | #define OM_MARK_AS_STATIC(addr) do {} while (0) |
---|
[e70e45] | 21 | #endif |
---|
| 22 | |
---|
[cadaf2] | 23 | #if OM_PROVIDE_MALLOC > 0 |
---|
| 24 | |
---|
[e70e45] | 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; |
---|
[13fe1b] | 30 | |
---|
[e70e45] | 31 | size = size*nmemb; |
---|
| 32 | omTypeAlloc0Aligned(void*, addr, size); |
---|
| 33 | OM_MARK_AS_STATIC(addr); |
---|
| 34 | return addr; |
---|
| 35 | } |
---|
[13fe1b] | 36 | |
---|
[e70e45] | 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 | |
---|
[d9f665] | 49 | #if defined(sgi) |
---|
| 50 | void* memalign(size_t size_1, size_t size_2) |
---|
[16643ec] | 51 | #elif (defined(__sun) && (defined(__sparc) || defined(__i386) || defined(__x86_64)) || defined(__CYGWIN__)) |
---|
[45e70f] | 52 | void* memalign(size_t size_1, size_t size_2) |
---|
[d9f665] | 53 | #else |
---|
[e70e45] | 54 | void* memalign(void* alignment, size_t size) |
---|
[d9f665] | 55 | #endif |
---|
[e70e45] | 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 | } |
---|
[13fe1b] | 71 | else |
---|
[e70e45] | 72 | { |
---|
| 73 | free(old_addr); |
---|
| 74 | return malloc(new_size); |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
[d83977] | 78 | /* on some systems strdup is a macro -- replace it unless OMALLOC_FUNC |
---|
| 79 | is defined */ |
---|
[900cea] | 80 | #ifndef OMALLOC_USES_MALLOC |
---|
| 81 | #if !defined(OMALLOC_FUNC) |
---|
[6eaa71] | 82 | #undef strdup |
---|
[d83977] | 83 | #endif |
---|
[09f230] | 84 | char* strdup_(const char* addr) |
---|
[e70e45] | 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 | } |
---|
[900cea] | 95 | #endif |
---|
[cadaf2] | 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 | |
---|
[e70e45] | 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 | } |
---|
[13fe1b] | 122 | else |
---|
[e70e45] | 123 | { |
---|
| 124 | freeSize(old_addr, old_size); |
---|
| 125 | return malloc(new_size); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | #endif |
---|