File: pmalloc.c

package info (click to toggle)
biew 5.6.2-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,004 kB
  • ctags: 7,157
  • sloc: ansic: 50,860; asm: 809; makefile: 396; pascal: 371
file content (192 lines) | stat: -rw-r--r-- 4,268 bytes parent folder | download | duplicates (2)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
 * @namespace   biewlib
 * @file        biewlib/pmalloc.c
 * @brief       This file contains implementation of preemptive memory allocation.
 * @version     -
 * @remark      this source file is part of Binary vIEW project (BIEW).
 *              The Binary vIEW (BIEW) is copyright (C) 1995 Nick Kurshev.
 *              All rights reserved. This software is redistributable under the
 *              licence given in the file "Licence.en" ("Licence.ru" in russian
 *              translation) distributed in the BIEW archive.
 * @note        Requires POSIX compatible development system
 *
 * @author      Nick Kurshev
 * @since       1999
 * @note        Development, fixes and improvements
**/
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "biewlib/sysdep/__config.h"
#if __WORDSIZE == 16
#include <malloc.h>
#endif
#include "biewlib/pmalloc.h"

#if !defined( NDEBUG ) && __WORDSIZE == 16 && defined(__TSC__)
/* 16-bits protected mode it's only way of quality stress test of code */
#include <tsxlib.h>
#include <dos.h>
#define malloc(s)       (MK_FP(AllocSeg(s),0))
#define realloc(p,s)    (ReallocSeg(s,FP_SEG(p)) ? p : 0)
#define free(p)         (FreeSeg(FP_SEG(p)))
extern  unsigned        _MaxReallocHugeSegments;
#ifdef halloc
#undef halloc
#endif
#define halloc(s)       (MK_FP(HugeAllocSeg((unsigned)s,(unsigned)(s>>16),_MaxReallocHugeSegments),0))
#ifdef hrealloc
#undef hrealloc
#endif
#define hrealloc(p,s)   (HugeReallocSeg((unsigned)s,(unsigned)(s>>16),FP_SEG(p)) ? p : 0)
#ifdef hfree
#undef hfree
#endif
#define hfree(p)        (FreeSeg(FP_SEG(p)))
#endif
static LowMemCallBack *lmstack = NULL;
unsigned               lmcount = 0;

tBool    __FASTCALL__ PMRegLowMemCallBack(LowMemCallBack func)
{
  void *new_ptr;
  tBool ret = False;
  if(lmcount < UINT_MAX)
  {
    if(!lmstack) new_ptr = malloc(sizeof(LowMemCallBack));
    else         new_ptr = realloc(lmstack,(lmcount+1)*sizeof(LowMemCallBack));
    if(!new_ptr) return 0;
    lmstack = new_ptr;
    lmstack[lmcount++] = func;
    ret = True;
  }
  return ret;
}

tBool  __FASTCALL__ PMUnregLowMemCallBack(LowMemCallBack func)
{
  void *new_ptr;
  unsigned i,fidx;
  tBool ret;
  fidx = UINT_MAX;
  for(i = 0;i < lmcount;i++)
  {
    if(lmstack[i] == func)
    {
      fidx = i;
      break;
    }
  }
  ret = False;
  if(fidx != UINT_MAX)
  {
    memmove(&lmstack[fidx],&lmstack[fidx+1],lmcount-(fidx+1));
    new_ptr = realloc(lmstack,(lmcount--)*sizeof(LowMemCallBack));
    if(new_ptr) lmstack = new_ptr;
    ret = True;
  }
  return ret;
}

void *           __FASTCALL__ PMalloc(size_t obj_size)
{
  void *ret;
  unsigned i;
  ret = malloc(obj_size);
  if(!ret)
  {
    for(i = 0;i < lmcount;i++)
    {
      if(lmstack[i](obj_size))
      {
        ret = malloc(obj_size);
        if(ret) break;
      }
    }
  }
  return ret;
}

void *           __FASTCALL__ PRealloc(void *ptr,size_t obj_size)
{
  void *ret;
  unsigned i;
  ret = realloc(ptr,obj_size);
  if(!ret)
  {
    for(i = 0;i < lmcount;i++)
    {
      if(lmstack[i](obj_size))
      {
        ret = realloc(ptr,obj_size);
        if(ret) break;
      }
    }
  }
  return ret;
}

void             __FASTCALL__ PFree(void *ptr)
{
  free(ptr);
}

#if __WORDSIZE == 16
void __HUGE__ *  __FASTCALL__ PHMalloc(unsigned long obj_size)
{
  void __HUGE__ *ret;
  unsigned i;
  ret = halloc(obj_size);
  if(!ret)
  {
    for(i = 0;i < lmcount;i++)
    {
      if(lmstack[i](obj_size))
      {
        ret = halloc(obj_size);
        if(ret) break;
      }
    }
  }
  return ret;
}

void __HUGE__ *  __FASTCALL__ PHRealloc(void __HUGE__ *ptr,unsigned long obj_size)
{
  void __HUGE__ *ret;
  unsigned i;
  ret = hrealloc(ptr,obj_size);
  if(!ret)
  {
    for(i = 0;i < lmcount;i++)
    {
      if(lmstack[i](obj_size))
      {
        ret = hrealloc(ptr,obj_size);
        if(ret) break;
      }
    }
  }
  return ret;
}

void             __FASTCALL__ PHFree(void __HUGE__ * ptr)
{
  hfree(ptr);
}
#else
void __HUGE__ *  __FASTCALL__ PHMalloc(unsigned long obj_size)
{
  return PMalloc(obj_size);
}

void __HUGE__ *  __FASTCALL__ PHRealloc(void __HUGE__ *ptr,unsigned long obj_size)
{
  return PRealloc(ptr,obj_size);
}

void             __FASTCALL__ PHFree(void __HUGE__ * ptr)
{
  PFree(ptr);
}
#endif