source: git/omalloc/omalloc.c @ 8f7098

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