ram-smalloc.c 5.85 KB
Newer Older
Linus Tolke's avatar
Linus Tolke committed
1
/*
Per Cederqvist's avatar
Per Cederqvist committed
2
 * $Id: ram-smalloc.c,v 0.13 1993/10/13 00:12:43 ceder Exp $
Linus Tolke's avatar
Linus Tolke committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * Copyright (C) 1991  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM 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 1, or (at your option) 
 * any later version.
 * 
 * LysKOM 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 LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25
26
27
28
29
30
31
32
/*
 * smalloc.c
 *
 * Contains memory allocator routines
 *
 *	TEST VERSION by ceder
 */

33
34
35
/*
 * Define DEBUG_MALLOC to get traces from smalloc, srealloc and sfree.
 * Run the resulting lyskomd under gdb. Source trace-mem.gdb. Run
Per Cederqvist's avatar
Per Cederqvist committed
36
 * M-x resolve-trace (from handle-malloc-dump.el) on the resulting output.
37
 */
Per Cederqvist's avatar
Per Cederqvist committed
38
/* #define DEBUG_MALLOC */
39

Per Cederqvist's avatar
Per Cederqvist committed
40
static char *rcsid = "$Id: ram-smalloc.c,v 0.13 1993/10/13 00:12:43 ceder Exp $";
41
42
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
43

Per Cederqvist's avatar
Per Cederqvist committed
44
45
46
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
47
48
#include <stdio.h>

Per Cederqvist's avatar
Per Cederqvist committed
49
#include "exp.h"
Per Cederqvist's avatar
Per Cederqvist committed
50
51
52
#include "kom-types.h"
#include "lyskomd.h"
#include "server/smalloc.h"
Per Cederqvist's avatar
Per Cederqvist committed
53

54
static int no_of_allocated_blocks = 0;
Per Cederqvist's avatar
Per Cederqvist committed
55

56
57
58
59
60
61
62
63
64
65
#ifdef DEBUG_MALLOC
static void
trace_smalloc(size_t size,
	      void *result)
{
    printf("smalloc:\nArg: 0x%lx\nRes: 0x%lx\n", (long)size, (long)result);
    printf("==== end ====\n");
}
#endif

Per Cederqvist's avatar
Per Cederqvist committed
66
67
68
69
/*
 * "safe" malloc. Handles the case when malloc returns NULL.
 * smalloc cannot fail.
 */
Per Cederqvist's avatar
Per Cederqvist committed
70
EXPORT  void *
Per Cederqvist's avatar
Per Cederqvist committed
71
72
73
74
75
76
smalloc(size_t size)
{
   unsigned int *p;
   
   p = (unsigned int *) malloc(size + 2*sizeof(unsigned int) + 2);
   if (p == NULL)
Per Cederqvist's avatar
Per Cederqvist committed
77
       restart_kom("Can't allocate %lu bytes.\n", (unsigned long)size);
Per Cederqvist's avatar
Per Cederqvist committed
78
79
80
81
82
83
84

   ++no_of_allocated_blocks;

   *p++ = SMALLOC_MAGIC_ALLOC;
   *p++ = size;
   ((unsigned char *) p)[size]   = 0x89;
   ((unsigned char *) p)[size+1] = 0xA7;
85
86
87
88
89

#ifdef DEBUG_MALLOC
   trace_smalloc(size, p);
#endif

Per Cederqvist's avatar
Per Cederqvist committed
90
91
92
   return (void *) p;
}

93
94
95
96
97
98
99
100
#ifdef DEBUG_MALLOC
static void
trace_free(void *block)
{
    printf("sfree:\nArg: 0x%lx\n", (long)block);
    printf("==== end ====\n");
}
#endif
Per Cederqvist's avatar
Per Cederqvist committed
101

Per Cederqvist's avatar
Per Cederqvist committed
102
EXPORT  void
Per Cederqvist's avatar
Per Cederqvist committed
103
104
105
106
107
108
109
110
111
112
113
sfree(void * ptr)	/* it is legal to sfree a NULL pointer */
{
    unsigned int *ip;

#if 0
    if ( ptr == NULL)
        log("SFREE: Freeing a NULL pointer - should be bad! /pen\n");
#endif
    
    if ( ptr != NULL )
    {
114
115
116
#ifdef DEBUG_MALLOC
	trace_free(ptr);
#endif
Per Cederqvist's avatar
Per Cederqvist committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
        ip = (unsigned int *) ptr;
	ip -= 2;
	switch (*ip)
	{
	    case SMALLOC_MAGIC_ALLOC:
		if (((unsigned char *) (ip+2))[ip[1]] != 0x89 ||
		    ((unsigned char *) (ip+2))[ip[1]+1] != 0xA7)
		  restart_kom("SFREE: Buffer overflow, bsize = %ul\n", ip[1]);
	        --no_of_allocated_blocks;
		*ip = SMALLOC_MAGIC_FREE;
		free( ip );
		break;

	    case SMALLOC_MAGIC_FREE:
		restart_kom("SFREE: Trying to free already freed block\n");

	    default:
		restart_kom("SFREE: Illegal magic number\n");
	}
    }
}

139
140
141
142
143
144
145
146
147
148
149
#ifdef DEBUG_MALLOC
static void
trace_srealloc(size_t size,
	       void *arg,		   
	       void *result)
{
    printf("srealloc:\nSize: 0x%lx\nArg: 0x%lx\nRes: 0x%lx\n",
	   (long)size, (long)arg, (long)result);
    printf("==== end ====\n");
}
#endif
Per Cederqvist's avatar
Per Cederqvist committed
150
EXPORT  void *
Per Cederqvist's avatar
Per Cederqvist committed
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
srealloc(void * ptr, size_t size) /* Never fails. It is legal to */
{				    /* realloc the NULL ptr. */
    unsigned int   * ip;
    unsigned int   * new_ptr;

    if ( ptr == NULL )
	return smalloc(size);

    ip = (unsigned int *) ptr;
    ip -= 2;
    switch (*ip)
    {
        case SMALLOC_MAGIC_ALLOC:
            break;

	case SMALLOC_MAGIC_FREE:
	    restart_kom("SREALLOC: Trying to realloc freed block\n");

	default:
	    restart_kom("SREALLOC: Illegal magic number\n");
    }

    if (((unsigned char *) (ip+2))[ip[1]] != 0x89 ||
	((unsigned char *) (ip+2))[ip[1]+1] != 0xA7)
      restart_kom("SREALLOC: Buffer overflow, osize = %ul, nsize = %ul\n",
		  ip[1], size);

    *ip = SMALLOC_MAGIC_FREE;
    if ( (new_ptr = (unsigned int *) realloc((void *) ip,
				size+2*sizeof(unsigned int)+2) ) == NULL )
    {
	restart_kom("Out of memory - can't realloc. ptr = %d size = %d. ",
		    (int)ptr, size);
    }

    *new_ptr++ = SMALLOC_MAGIC_ALLOC;
    *new_ptr++ = size;

    ((unsigned char *) new_ptr)[size]   = 0x89;
    ((unsigned char *) new_ptr)[size+1] = 0xA7;
191
192
193
194
195

#ifdef DEBUG_MALLOC
    trace_srealloc(size, ptr, new_ptr);
#endif

Per Cederqvist's avatar
Per Cederqvist committed
196
197
198
199
200
201
202
203
204
205
206
207
208
    return (void *) new_ptr;
}


/*
 * Allocate temporary memory, which is automatically freed after this
 * atomic call.
 */

static void **tmp_alloc_table   = NULL;
static int tmp_alloc_table_size = 0; /* Size */
static int tmp_alloc_table_use  = 0; /* Used size */

Per Cederqvist's avatar
Per Cederqvist committed
209
EXPORT  void *
Per Cederqvist's avatar
Per Cederqvist committed
210
tmp_alloc(unsigned long size)
Per Cederqvist's avatar
Per Cederqvist committed
211
212
213
214
215
216
217
218
{
    if ( tmp_alloc_table_size <= tmp_alloc_table_use )
    {
	/* Need to increas table. */
	tmp_alloc_table = srealloc (tmp_alloc_table,
				    ((++tmp_alloc_table_size)
				     * sizeof (void *)));
    }
219

Per Cederqvist's avatar
Per Cederqvist committed
220
221
222
223
224
225
226
227
228
    return (tmp_alloc_table[ tmp_alloc_table_use++ ]
	    = smalloc (size));
}


/*
 * Free all core which is allocated with tmp_alloc(). This is called from
 * end_of_atomic().
 */
Per Cederqvist's avatar
Per Cederqvist committed
229
EXPORT void
Per Cederqvist's avatar
Per Cederqvist committed
230
231
232
233
234
235
236
237
238
239
240
241
242
free_tmp(void)
{
    int i;

    for ( i = 0; i < tmp_alloc_table_use; i++ )
    {
	sfree ( tmp_alloc_table[ i ] );
	tmp_alloc_table[ i ] = NULL;
    }

    tmp_alloc_table_use = 0;
}

Per Cederqvist's avatar
Per Cederqvist committed
243
EXPORT  void
Per Cederqvist's avatar
Per Cederqvist committed
244
245
246
247
248
249
250
free_all_tmp(void)
{
    free_tmp();
    sfree( tmp_alloc_table );
    tmp_alloc_table = NULL;
    tmp_alloc_table_size = 0;
}
Per Cederqvist's avatar
Per Cederqvist committed
251
252
253
254

EXPORT void
dump_smalloc_counts(FILE *stat_file)
{
255
256
    fprintf(stat_file, "---ram-smalloc.c:\n"
	    "\tAllocated blocks (grand total): %d\n",
Per Cederqvist's avatar
Per Cederqvist committed
257
258
	    no_of_allocated_blocks);
}