File: morecore.c

package info (click to toggle)
swi-prolog 3.1.0-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 8,772 kB
  • ctags: 12,869
  • sloc: ansic: 43,657; perl: 12,577; lisp: 4,359; sh: 1,534; makefile: 798; awk: 14
file content (140 lines) | stat: -rw-r--r-- 3,167 bytes parent folder | download | duplicates (4)
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
/*  $Id: morecore.c,v 1.2 1996/04/19 11:47:59 jan Exp $

    Part of XPCE
    Designed and implemented by Anjo Anjewierden and Jan Wielemaker
    E-mail: jan@swi.psy.uva.nl

    Copyright (C) 1995 University of Amsterdam. All rights reserved.
*/

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This little module is included into   pl-save.c if FORCED_MALLOC_BASE is
defined. It requires the GNU implementation   of malloc() and friends, a
mmap() that handles MAP_ANON  (can  be  changed)   and  can  map  at the
addresses normally used at the target machine.  I think this module will
only work if you run Linux :-)  It   is  implemented to debug the win32s
version, allocating from 0x80000000L.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
extern int getpagesize(void);		/* no prototype */

#if 0					/* include if not defined (old libc) */
extern void * (*__morecore)(long size);
#endif

static void *base;			/* base */
static void *top;			/* external top */
static void *atop;			/* allocated top */
static int  pagsize;

#ifndef roundup
#define roundup(p, n) (((p)+(n)-1 / (n)) * (n))
#endif

#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
#define MAP_ANON MAP_ANONYMOUS
#endif

#define get_map_fd() (-1)
#define STACK_MAP_TYPE MAP_ANON|MAP_PRIVATE|MAP_FIXED

void *
mycore(long size)			/* similar to sbrk() */
{ void *mem = top;

  if ( top + size >= atop )
  { void *ntop = (void *)roundup((ulong) top + size, pagsize);
    if ( mmap(atop, ntop - atop,
	      PROT_READ|PROT_WRITE, STACK_MAP_TYPE, -1, 0L) != atop )
    { fprintf(stderr, "mycore(): %s\n", strerror(errno));
      return NULL;
    }

    atop = ntop;
  }

  top = top + size;
  
  return mem;
}


void
start_memory(void * address)
{ pagsize    = getpagesize();

  base       = (void *)roundup((ulong) address, pagsize);
  top	     = base;
  atop	     = base;
  __morecore = mycore;
}

#ifdef TEST

#ifndef HAVE_STRTOUL
ulong
strtoul(const char *s, char **eptr, int base)
{ static unsigned char xmap[256];
  static int xmap_initialised = 0;
  unsigned long val;

  if ( !xmap_initialised )
  { int n;

    xmap_initialised++;
    for(n=0; n<256; n++)
      xmap[n] = 0xff;
    for(n='0'; n<='9'; n++)
      xmap[n] = n - '0';
    for(n='a'; n<='z'; n++)
      xmap[n] = n - 'a' + 10;
    for(n='A'; n<='Z'; n++)
      xmap[n] = n - 'A' + 10;
  }

  if ( s[0] == '0' && (s[1] == 'x' || s[1] == 'X') )
    s += 2;

  for(val = 0; *s; s++)
  { unsigned long dval = xmap[*s];

    if ( dval < (unsigned) base )
      val = val * base + dval;
    else
    { if ( eptr )
	*eptr = (char *)s;
    }
  }

  return val;
}
#endif


main(int argc, char **argv)
{ static int testvals[] = { 1, 1000, 10000, 100000, -1 };
  int *tv = testvals;
  ulong base = 0x40000000L;

  if ( argc == 2 )
    base = strtoul(argv[1], NULL, 16);

  start_memory((void *)base);

  for(; *tv != -1; tv++)
  { char *rval = malloc(*tv);

    printf("malloc(%-6d) --> 0x%08x\n", *tv, rval);
    memset(rval, 0xbf, *tv);
  }

  return 0;
}

#endif