source: git/omalloc/omalloc.c

spielwiese
Last change on this file was d9cea7, checked in by Hans Schoenemann <hannes@…>, 3 years ago
omalloc: removed memalign
  • Property mode set to 100644
File size: 2.2 KB
Line 
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.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
25void* 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
37void free(void* addr)
38{
39  omfree(addr);
40}
41
42void* valloc(size_t size)
43{
44  fputs("omalloc Warning: valloc not yet implemented\n",stderr);
45  fflush(NULL);
46  return NULL;
47}
48
49void* realloc(void* old_addr, size_t new_size)
50{
51  if (old_addr && new_size)
52  {
53    void* new_addr;
54    omTypeReallocAligned(old_addr, void*, new_addr, new_size);
55    OM_MARK_AS_STATIC(new_addr);
56    return new_addr;
57  }
58  else
59  {
60    free(old_addr);
61    return malloc(new_size);
62  }
63}
64
65/* on some systems strdup is a macro -- replace it unless OMALLOC_FUNC
66   is defined */
67#ifndef OMALLOC_USES_MALLOC
68#if !defined(OMALLOC_FUNC)
69#undef strdup
70#endif
71char* strdup_(const char* addr)
72{
73  char* n_s;
74  if (addr)
75  {
76    n_s = omStrDup(addr);
77    OM_MARK_AS_STATIC(n_s);
78    return n_s;
79  }
80  return NULL;
81}
82#endif
83#endif
84
85void* malloc(size_t size)
86{
87  void* addr;
88  if (size == 0) size = 1;
89
90  omTypeAllocAligned(void*, addr, size);
91  OM_MARK_AS_STATIC(addr);
92  return addr;
93}
94
95void freeSize(void* addr, size_t size)
96{
97  if (addr) omFreeSize(addr, size);
98}
99
100void* reallocSize(void* old_addr, size_t old_size, size_t new_size)
101{
102  if (old_addr && new_size)
103  {
104   void* new_addr;
105    omTypeReallocAlignedSize(old_addr, old_size, void*, new_addr, new_size);
106    OM_MARK_AS_STATIC(new_addr);
107    return new_addr;
108  }
109  else
110  {
111    freeSize(old_addr, old_size);
112    return malloc(new_size);
113  }
114}
115#endif
Note: See TracBrowser for help on using the repository browser.