File: getmem.c

package info (click to toggle)
zmailer 2.99.51.52pre3-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 16,596 kB
  • ctags: 7,422
  • sloc: ansic: 90,470; sh: 3,608; makefile: 2,784; perl: 1,585; python: 115; awk: 22
file content (126 lines) | stat: -rw-r--r-- 2,965 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
/*  Author: Mark Moraes <moraes@csri.toronto.edu> */

/*LINTLIBRARY*/

#include "defs.h"
#include "globals.h"

RCSID("$Id: getmem.c,v 1.1.1.1 1998/02/10 21:01:46 mea Exp $")

/* gets memory from the system via the sbrk() system call.  Most Un*xes */
univptr_t
_mal_sbrk(nbytes)
size_t nbytes;
{
	static char *lastsbrk = NULL;
	char *p;

	p = (char *) sbrk((int) nbytes);
	if (p == (char*)-1L) {
	  return p; /* Ran out of memory! */
	}
	/*
	 * Some old SVR3 apparently have a kernel bug: after several sbrk
	 * calls, sbrk suddenly starts returning values lower than the ones it
	 * returned before.  Yeesh!
	 */
	if (nbytes > 0) {
	    ASSERT(p > lastsbrk, "system call error? sbrk returned value lower than previous calls");
	    lastsbrk = p;
	}
	return p;
}

/*
 * gets memory from the system via mmaping a file.  This was written for SunOS
 * versions greater than 4.0.  The filename is specified by the environment
 * variable CSRIMALLOC_MMAPFILE or by the call to mal_mmapset().  Using this
 * instead of sbrk() has the advantage of bypassing the swap system, allowing
 * processes to run with huge heaps even on systems configured with small swap
 * space.
 */
static char *mmap_filename;

#ifdef HAVE_MMAP
/* Sun gets size_t wrong, and these follow, thanks to my #defines! */
#undef caddr_t
/* #undef size_t */
#undef u_char
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/file.h>

univptr_t
_mal_mmap(nbytes)
size_t nbytes;
{
	static struct {
		int i_fd;
		caddr_t i_data;
		caddr_t i_end;
		size_t i_size;
		size_t i_alloced;
	} mmf;
	struct stat stbuf;

	if (mmf.i_data != NULL) {
		/* Already initialized & mmaped the file */
		univptr_t p = mmf.i_data + mmf.i_alloced;
		
		if ((char *) p + nbytes > mmf.i_end) {
			errno = ENOMEM;
			return (univptr_t) -1;
		}
		mmf.i_alloced += nbytes;
		return p;
	}

	/*
	 * This code is run the first time the function is called, it opens
	 * the file and mmaps the
	 */
	if (mmap_filename == NULL) {
		mmap_filename = getenv("CSRIMALLOC_MMAPFILE");
		if (mmap_filename == NULL) {
			errno = ENOMEM;
			return (univptr_t) -1;
		}
	}

	mmf.i_fd = open(mmap_filename, O_RDWR, 0666);
	if (mmf.i_fd < 0 || fstat(mmf.i_fd, &stbuf) < 0)
		return (univptr_t) -1;
	if (stbuf.st_size < nbytes) {
		errno = ENOMEM;
		return (univptr_t) -1;
	}
	mmf.i_size = stbuf.st_size;
	mmf.i_data = mmap((caddr_t) 0, mmf.i_size, PROT_READ|PROT_WRITE,
			  MAP_SHARED, mmf.i_fd, (off_t) 0);
	if (mmf.i_data == (caddr_t) -1)
		return (univptr_t) -1;
	mmf.i_end = mmf.i_data + mmf.i_size;
	mmf.i_alloced = nbytes;
#if	defined(MADV_RANDOM) && !defined(NO_MADVISE)
	/* Advise vm system of random access pattern */
	(void) madvise(mmf.i_data, mmf.i_size, MADV_RANDOM);
#endif
	return mmf.i_data;
}
#else /* !HAVE_MMAP */
univptr_t
_mal_mmap(nbytes)
size_t nbytes;
{
	return (univptr_t) -1;
}
#endif /* HAVE_MMAP */

void
mal_mmap(fname)
char *fname;
{
	_malloc_memfunc = _mal_mmap;
	mmap_filename = fname;
}