File: allocx.c

package info (click to toggle)
clif 0.93-9.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 4,228 kB
  • sloc: ansic: 31,464; yacc: 5,067; lex: 819; makefile: 383; sh: 48
file content (171 lines) | stat: -rw-r--r-- 2,996 bytes parent folder | download | duplicates (7)
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
/*
    Clif - A C-like Interpreter Framework
    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997 T. Hruz, L. Koren
    1998 L. Koren

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
/*
 * allocx.c
 *
 * calloc interface
 *
 */

#include <stdio.h>
#include "config.h"
#include "global.h"
#ifdef STDC_HEADERS
#include <stdlib.h>
#else
#include <malloc.h>
#endif
#ifdef HAVE_ASSERT_H
#include <assert.h>
#endif

#include "allocx.h"

#define MASK 7
#define PAGE 1024
#define K 10

union align
{
  long l;
  char *p;
  double d;
  int (*f) PROTO((void));
};

struct pool_chunk
{
  struct pool_chunk *next;
  char *position;
  char *limit;
};

union header
{
  struct pool_chunk p;
  union align a;
};

static struct pool_chunk first[] = {{ NULL }, { NULL }},
		       *pool[] = { &first[0], &first[1]},
  *freeblock;

char *
callocx (n, size)
     unsigned n, size;
{
  return((char *)calloc(n,size));
}

char *
allocate (n, a)
     unsigned n,a;
{
  register struct pool_chunk *cp;

#ifdef HAVE_ASSERT_H
  assert (a < sizeof(pool)/sizeof(pool[0]));
#endif

  cp = pool[a];

  if (n & MASK)
    n += 8 - (n & MASK);
    
  while (cp->position + n > cp->limit)
    {
      if (NULL != (cp->next = freeblock))
	{
	  freeblock = freeblock->next;
	  cp = cp->next;
	}
      else
	{
	  unsigned m = sizeof(union header) + n + K * PAGE;
	  cp->next = (struct pool_chunk *)callocx (1, m);
	  cp = cp->next;
	  if (NULL == cp)
	    {
	      error_message (4002);
	      exit (1);
	    }
	  cp->limit = (char *)cp + m;
	}
      cp->position = (char *)(cp + sizeof (union header));
      cp->next = NULL;
      pool[a] = cp;
    }
  cp->position += n;
  return cp->position - n;
}

void
deallocate (a)
  unsigned a;
{
#ifdef HAVE_ASSERT_H
  assert (a < sizeof(pool)/sizeof(pool[0]));
#endif

  pool[a]->next = freeblock;
  freeblock = first[a].next;
  first[a].next = NULL;
  pool[a] = &first[a];
}

void
init_zero (p, n)
  char *p;
  unsigned n;
{
  while (n--)
    *p++ = 0;
}

void *
mallocx (size)
  unsigned int size;
{
  register void *res = malloc (size);

  if (res == NULL)
    error_message (4002);
  return res;
}

void *
reallocx (ptr, size)
  void *ptr;
  unsigned size;
{
  register void *res = realloc (ptr, size);

  if (res == NULL)
    error_message (4002);
  return res;
}

void
freex (ptr)
  void *ptr;
{
  return (free (ptr));
}