File: mem.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (318 lines) | stat: -rw-r--r-- 7,209 bytes parent folder | download
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Copyright (c) 1996, 1998, 1999 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit 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 GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */

/*
 * Memory object. Mostly an interface to the LMM. The malloc implementation
 * in the C library will look for this interface in the registry, and use it
 * to allocate memory. 
 */
#include <oskit/com.h>
#include <oskit/com/mem.h>
#include <oskit/com/services.h>
#include <oskit/lmm.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>			/* mem_lock - BOGUS! */
#include <oskit/machine/phys_lmm.h>

/* A common symbol that is overridden by the kernel library */
lmm_t			malloc_lmm;

/* the COM object */
static struct oskit_mem_ops mem_ops;
static oskit_mem_t	oskit_mem  = { &mem_ops };

/*
 * Always allocate multiples of MIN_INCR bytes.
 */
#define MIN_INCR	(16*1024)

#define MORECORE(size, flags) ({ \
	void *__mem;							  \
	oskit_size_t __size = ((size) + MIN_INCR - 1) & ~(MIN_INCR - 1);  \
									  \
	mem_unlock();							  \
	if ((__mem = oskit_mem_morecore(__size, flags)) == 0) {		  \
		return 0;						  \
        }								  \
	mem_lock();							  \
	lmm_add_free(&malloc_lmm, __mem, __size);			  \
}) 

#if 0
#define DPRINTF(fmt, args... ) printf(__FUNCTION__  ":" fmt , ## args)
#else
#define DPRINTF(fmt, args... )
#endif

static OSKIT_COMDECL 
mem_query(oskit_mem_t *m, const oskit_iid_t *iid, void **out_ihandle)
{ 
        if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &oskit_mem_iid, sizeof(*iid)) == 0) {
                *out_ihandle = m;
                return 0; 
        }
  
        *out_ihandle = 0;
        return OSKIT_E_NOINTERFACE;
};

static OSKIT_COMDECL_U
mem_addref(oskit_mem_t *m)
{
	/* No reference counting; only one of them */
	return 1;
}

static OSKIT_COMDECL_U
mem_release(oskit_mem_t *m)
{
	/* No reference counting; only one of them */
	return 1;
}

static void * OSKIT_COMCALL
mem_alloc(oskit_mem_t *m, oskit_u32_t size, oskit_u32_t flags)
{
	lmm_flags_t	lmm_flags = 0;
	oskit_u32_t	*chunk;

	if (flags & OSKIT_MEM_ISADMA_MEM)
		lmm_flags |= LMMF_16MB;

	mem_lock();
	if (flags & OSKIT_MEM_AUTO_SIZE) {
		size += sizeof(oskit_u32_t);

		while (!(chunk = lmm_alloc(&malloc_lmm, size, lmm_flags))) {
			MORECORE(size, lmm_flags);
		}

		*chunk++ = size;
	}
	else {
		while (!(chunk = lmm_alloc(&malloc_lmm, size, lmm_flags))) {
			MORECORE(size, lmm_flags);
		}
	}
	mem_unlock();

	return chunk;
}

static void * OSKIT_COMCALL
mem_realloc(oskit_mem_t *m, void *ptr,
	    oskit_u32_t oldsize, oskit_u32_t newsize, oskit_u32_t flags)
{
	lmm_flags_t	lmm_flags = 0;
	oskit_u32_t	*chunk;

	if (flags & OSKIT_MEM_ISADMA_MEM)
		lmm_flags |= LMMF_16MB;

	mem_lock();
	if (flags & OSKIT_MEM_AUTO_SIZE) {
		oskit_size_t	*op;

		op = (oskit_size_t *)ptr;
		oldsize  = *--op;
		newsize += sizeof(oskit_u32_t);

		while (!(chunk = lmm_alloc(&malloc_lmm, newsize, lmm_flags))) {
			MORECORE(newsize, lmm_flags);
		}

		DPRINTF("0: %p %p %d %d\n", ptr, chunk, oldsize, newsize);

		memcpy(chunk, op, oldsize < newsize ? oldsize : newsize);
		lmm_free(&malloc_lmm, op, oldsize);

		*chunk++ = newsize;
	}
	else {
		while (!(chunk = lmm_alloc(&malloc_lmm, newsize, lmm_flags))) {
			MORECORE(newsize, lmm_flags);
		}
		
		DPRINTF("1: %p %p %d %d\n", ptr, chunk, oldsize, newsize);

		memcpy(chunk, ptr, oldsize < newsize ? oldsize : newsize);
		lmm_free(&malloc_lmm, ptr, oldsize);
	}
	mem_unlock();

	return chunk;
}

static void *OSKIT_COMCALL
mem_alloc_aligned(oskit_mem_t *m,
		  oskit_u32_t size, oskit_u32_t flags, oskit_u32_t align)
{
	lmm_flags_t	lmm_flags = 0;
	oskit_u32_t	*chunk;
	unsigned	shift;

	/* Find the alignment shift in bits.  XXX use proc_ops.h  */
	for (shift = 0; (1 << shift) < align; shift++);

	if (flags & OSKIT_MEM_ISADMA_MEM)
		lmm_flags |= LMMF_16MB;

	mem_lock();
	if (flags & OSKIT_MEM_AUTO_SIZE) {
		size += sizeof(oskit_u32_t);

		while (!(chunk =
			 lmm_alloc_aligned(&malloc_lmm, size, lmm_flags, shift,
					(1 << shift) - sizeof(oskit_u32_t)))) {
			MORECORE(size * 2, lmm_flags);
		}
		
		DPRINTF("0: %p %d %d\n", chunk, size, align);
		
		*chunk++ = size;
	}
	else {
		while (!(chunk = lmm_alloc_aligned(&malloc_lmm,
						size, lmm_flags, shift, 0))) {
			MORECORE(size * 2, lmm_flags);
		}

		DPRINTF("1: %p %d %d\n", chunk, size, align);
	}
	mem_unlock();

	return chunk;
}

static void OSKIT_COMCALL
mem_free(oskit_mem_t *m, void *ptr, oskit_u32_t size, oskit_u32_t flags)
{
	/* Posix says free of NULL does nothing */
	if (! ptr)
		return;

	mem_lock();
	if (flags & OSKIT_MEM_AUTO_SIZE) {
		oskit_u32_t *chunk = (oskit_u32_t *)ptr - 1;

		DPRINTF("0: %p %d 0x%x\n", ptr, *chunk, flags);
		
		lmm_free(&malloc_lmm, chunk, *chunk);
	}
	else {
		DPRINTF("1: %p %d 0x%x\n", ptr, size, flags);

		lmm_free(&malloc_lmm, ptr, size);
	}
	mem_unlock();
}

static oskit_u32_t OSKIT_COMCALL
mem_getsize(oskit_mem_t *m, void *ptr)
{
	if (ptr) {
		oskit_u32_t *chunk = (oskit_u32_t *)ptr - 1;

		return *chunk;
	}
	return 0;
}

static void *OSKIT_COMCALL
mem_alloc_gen(oskit_mem_t *m,
	      oskit_u32_t size, oskit_u32_t flags,
	      oskit_u32_t align_bits, oskit_u32_t align_ofs)
{
	lmm_flags_t	lmm_flags = 0;
	oskit_u32_t	*chunk;

	if (flags & OSKIT_MEM_ISADMA_MEM)
		lmm_flags |= LMMF_16MB;

	/*
	 * Not really all that general. For now, support only non
	 * autosize allocations. 
	 */
	if (flags & OSKIT_MEM_AUTO_SIZE)
		return NULL;

	mem_lock();
	while (!(chunk = lmm_alloc_gen(&malloc_lmm, size, lmm_flags,
				       align_bits, align_ofs, 0, -1))) {
		MORECORE(size * 2, flags);
	}
	DPRINTF("1: %p %d %x %d %d\n", chunk, size, flags, align, align_ofs);
	mem_unlock();

	return chunk;
}

static oskit_size_t OSKIT_COMCALL
mem_avail(oskit_mem_t *m, oskit_u32_t flags)
{
	lmm_flags_t	lmm_flags = 0;
	oskit_size_t	avail;

	if (flags & OSKIT_MEM_ISADMA_MEM)
		lmm_flags |= LMMF_16MB;

	if (flags & OSKIT_MEM_X861MB_MEM)
		lmm_flags |= LMMF_1MB;

	mem_lock();
	avail = lmm_avail(&malloc_lmm, lmm_flags);
	mem_unlock();

	return avail;
}

static OSKIT_COMDECL_V
mem_dump(oskit_mem_t *m)
{
	mem_lock();
	lmm_dump(&malloc_lmm);
	mem_unlock();
}

static struct oskit_mem_ops mem_ops = {
	mem_query,
	mem_addref,
	mem_release,
	mem_alloc,
	mem_realloc,
	mem_alloc_aligned,
	mem_free,
	mem_getsize,
	mem_alloc_gen,
	mem_avail,
	mem_dump,
};

#ifdef CPUNITS
oskit_mem_t	*oskit_mem_object = &oskit_mem;
#else
/*
 * Not a lot to do in this impl.
 */
oskit_mem_t *
oskit_mem_init(void)
{
	return &oskit_mem;
}
#endif