File: mem-common.ins

package info (click to toggle)
smlnj 110.79-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 82,564 kB
  • sloc: ansic: 32,532; asm: 6,314; sh: 2,296; makefile: 1,821; perl: 1,170; pascal: 295; yacc: 190; cs: 78; python: 77; lisp: 19
file content (89 lines) | stat: -rw-r--r-- 1,747 bytes parent folder | download | duplicates (6)
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
/* mem-common.ins
 *
 * This is code that is common to all three implementations of the 
 * memory management library.
 */

#ifndef _MEM_COMMON_INS_
#define _MEM_COMMON_INS_

PVT Addr_t	PageSize;	/* the system page size. */
PVT Addr_t	PageShift;	/* PageSize == (1 << PageShift) */
PVT Addr_t	VMSizeB;	/* The amount of virtual memory allocated */

PVT status_t MapMemory (mem_obj_t *obj, Addr_t szb);
PVT void UnmapMemory (mem_obj_t *obj);


/* InitMemory:
 *
 * Initialize the common stuff.
 *
 */
PVT void InitMemory ()
{
    int		i, j;

    VMSizeB = 0;
    PageSize = GETPAGESIZE();
    for (i = 1, j = 0;  i != PageSize;  i <<= 1, j++)
	continue;
    PageShift = j;

} /* end of InitMemory */


/* MEM_GetVMSize:
 *
 * Return the amount of virtual memory (in K-bytes) allocated to the heap.
 */
long MEM_GetVMSize ()
{
    return (VMSizeB / ONE_K);

} /* end of MEM_GetVMSize */


/* MEM_AllocMemObj:
 * Get a new memory object from the O.S.  Return NIL on failure, otherwise return
 * a pointer to the object descriptor.
 */
mem_obj_t *MEM_AllocMemObj (Word_t szb)
{
    Word_t	alloc_szb;
    mem_obj_t	*obj;

    if ((obj = ALLOC_MEMOBJ()) == NIL(mem_obj_t *)) {
	Error ("unable to malloc chunk descriptor\n");
	return NIL(mem_obj_t *);
    }

    alloc_szb = (szb <= BIBOP_PAGE_SZB) ? BIBOP_PAGE_SZB : RND_MEMOBJ_SZB(szb);

    if (MapMemory (obj, alloc_szb) == FAILURE) {
	FREE_MEMOBJ (obj);
	return NIL(mem_obj_t *);
    }

    VMSizeB += alloc_szb;

    return obj;

} /* end of AllocMemObj */

/* MEM_FreeMemObj:
 */
void MEM_FreeMemObj (mem_obj_t *obj)
{
    if (obj == NIL(mem_obj_t *))
	return;

    UnmapMemory(obj);

    VMSizeB -= obj->sizeB;

    FREE_MEMOBJ (obj);

} /* end of MEM_FreeMemObj */

#endif /* !_MEM_COMMON_INS_ */