[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 |
---|
[16643ec] | 7 | * Version: $Id: omalloc.c,v 1.13 2008-08-22 10:32:19 wienand Exp $ |
---|
[e70e45] | 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; |
---|
[13fe1b] | 29 | |
---|
[e70e45] | 30 | size = size*nmemb; |
---|
| 31 | omTypeAlloc0Aligned(void*, addr, size); |
---|
| 32 | OM_MARK_AS_STATIC(addr); |
---|
| 33 | return addr; |
---|
| 34 | } |
---|
[13fe1b] | 35 | |
---|
[e70e45] | 36 | void* malloc(size_t size) |
---|
| 37 | { |
---|
| 38 | void* addr; |
---|
| 39 | if (size == 0) size = 1; |
---|
[13fe1b] | 40 | |
---|
[e70e45] | 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 | |
---|
[d9f665] | 58 | #if defined(sgi) |
---|
| 59 | void* memalign(size_t size_1, size_t size_2) |
---|
[16643ec] | 60 | #elif (defined(__sun) && (defined(__sparc) || defined(__i386) || defined(__x86_64)) || defined(__CYGWIN__)) |
---|
[45e70f] | 61 | void* memalign(size_t size_1, size_t size_2) |
---|
[d9f665] | 62 | #else |
---|
[e70e45] | 63 | void* memalign(void* alignment, size_t size) |
---|
[d9f665] | 64 | #endif |
---|
[e70e45] | 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 | } |
---|
[13fe1b] | 80 | else |
---|
[e70e45] | 81 | { |
---|
| 82 | free(old_addr); |
---|
| 83 | return malloc(new_size); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
[d83977] | 87 | /* on some systems strdup is a macro -- replace it unless OMALLOC_FUNC |
---|
| 88 | is defined */ |
---|
[900cea] | 89 | #ifndef OMALLOC_USES_MALLOC |
---|
| 90 | #if !defined(OMALLOC_FUNC) |
---|
[6eaa71] | 91 | #undef strdup |
---|
[d83977] | 92 | #endif |
---|
[09f230] | 93 | char* strdup_(const char* addr) |
---|
[e70e45] | 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 | } |
---|
[900cea] | 104 | #endif |
---|
[e70e45] | 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 | } |
---|
[13fe1b] | 119 | else |
---|
[e70e45] | 120 | { |
---|
| 121 | freeSize(old_addr, old_size); |
---|
| 122 | return malloc(new_size); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | #endif |
---|