File: omalloc.c

package info (click to toggle)
singular 1%3A4.4.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 47,520 kB
  • sloc: cpp: 319,164; ansic: 42,206; perl: 5,855; sh: 5,524; lisp: 4,241; python: 2,101; makefile: 1,890; yacc: 1,651; pascal: 1,411; lex: 1,367; tcl: 1,024; xml: 182
file content (115 lines) | stat: -rw-r--r-- 2,209 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*******************************************************************
 *  File:    omalloc.c
 *  Purpose: implementation of ANSI-C conforming malloc functions
 *           -- the real version
 *  Author:  obachman@mathematik.uni-kl.de (Olaf Bachmann)
 *  Created: 11/99
 *******************************************************************/

#include <stdlib.h>
#include <stdio.h>

#ifndef OMALLOC_C
#define OMALLOC_C

#include "omalloc.h"

#ifdef OM_MALLOC_MARK_AS_STATIC
#define OM_MARK_AS_STATIC(addr) omMarkAsStaticAddr(addr)
#else
#define OM_MARK_AS_STATIC(addr) do {} while (0)
#endif

#if OM_PROVIDE_MALLOC > 0

void* calloc(size_t nmemb, size_t size)
{
  void* addr;
  if (size == 0) size = 1;
  if (nmemb == 0) nmemb = 1;

  size = size*nmemb;
  omTypeAlloc0Aligned(void*, addr, size);
  OM_MARK_AS_STATIC(addr);
  return addr;
}

void free(void* addr)
{
  omfree(addr);
}

void* valloc(size_t size)
{
  fputs("omalloc Warning: valloc not yet implemented\n",stderr);
  fflush(NULL);
  return NULL;
}

void* realloc(void* old_addr, size_t new_size)
{
  if (old_addr && new_size)
  {
    void* new_addr;
    omTypeReallocAligned(old_addr, void*, new_addr, new_size);
    OM_MARK_AS_STATIC(new_addr);
    return new_addr;
  }
  else
  {
    free(old_addr);
    return malloc(new_size);
  }
}

/* on some systems strdup is a macro -- replace it unless OMALLOC_FUNC
   is defined */
#ifndef OMALLOC_USES_MALLOC
#if !defined(OMALLOC_FUNC)
#undef strdup
#endif
char* strdup_(const char* addr)
{
  char* n_s;
  if (addr)
  {
    n_s = omStrDup(addr);
    OM_MARK_AS_STATIC(n_s);
    return n_s;
  }
  return NULL;
}
#endif
#endif

void* malloc(size_t size)
{
  void* addr;
  if (size == 0) size = 1;

  omTypeAllocAligned(void*, addr, size);
  OM_MARK_AS_STATIC(addr);
  return addr;
}

void freeSize(void* addr, size_t size)
{
  if (addr) omFreeSize(addr, size);
}

void* reallocSize(void* old_addr, size_t old_size, size_t new_size)
{
  if (old_addr && new_size)
  {
   void* new_addr;
    omTypeReallocAlignedSize(old_addr, old_size, void*, new_addr, new_size);
    OM_MARK_AS_STATIC(new_addr);
    return new_addr;
  }
  else
  {
    freeSize(old_addr, old_size);
    return malloc(new_size);
  }
}
#endif