source: git/omalloc/omalloc.c @ e3d503

spielwiese
Last change on this file since e3d503 was 89e918, checked in by Hans Schönemann <hannes@…>, 20 years ago
*hannes: avoid redefines git-svn-id: file:///usr/local/Singular/svn/trunk@7007 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 2.4 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 *  Version: $Id: omalloc.c,v 1.9 2004-01-26 10:16:51 Singular 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
24void* 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
36void* 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
46void free(void* addr)
47{
48  omfree(addr);
49}
50
51void* 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)
59void* memalign(size_t size_1, size_t size_2)
60#else
61void* memalign(void* alignment, size_t size)
62#endif
63{
64  fprintf(stderr, "omalloc Warning: memalign not yet implemented\n");
65  fflush(NULL);
66  return NULL;
67}
68
69void* realloc(void* old_addr, size_t new_size)
70{
71  if (old_addr && new_size)
72  {
73    void* new_addr;
74    omTypeReallocAligned(old_addr, void*, new_addr, new_size);
75    OM_MARK_AS_STATIC(new_addr);
76    return new_addr;
77  }
78  else
79  {
80    free(old_addr);
81    return malloc(new_size);
82  }
83}
84
85/* on some systems strdup is a macro -- replace it unless OMALLOC_FUNC
86   is defined */
87#ifndef OMALLOC_USES_MALLOC
88#if !defined(OMALLOC_FUNC)
89#undef strdup
90#endif
91char* strdup(const char* addr)
92{
93  char* n_s;
94  if (addr)
95  {
96    n_s = omStrDup(addr);
97    OM_MARK_AS_STATIC(n_s);
98    return n_s;
99  }
100  return NULL;
101}
102#endif
103void freeSize(void* addr, size_t size)
104{
105  if (addr) omFreeSize(addr, size);
106}
107
108void* reallocSize(void* old_addr, size_t old_size, size_t new_size)
109{
110  if (old_addr && new_size)
111  {
112   void* new_addr;
113    omTypeReallocAlignedSize(old_addr, old_size, void*, new_addr, new_size);
114    OM_MARK_AS_STATIC(new_addr);
115    return new_addr;
116  }
117  else
118  {
119    freeSize(old_addr, old_size);
120    return malloc(new_size);
121  }
122}
123#endif
Note: See TracBrowser for help on using the repository browser.