File: morecore.c

package info (click to toggle)
swi-prolog 6.6.6-1~bpo70+1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 82,312 kB
  • sloc: ansic: 322,250; perl: 245,822; sh: 6,651; java: 5,254; makefile: 4,423; cpp: 4,153; ruby: 1,594; yacc: 843; xml: 82; sed: 12; sql: 6
file content (155 lines) | stat: -rw-r--r-- 3,892 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
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
/*  $Id$

    Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        jan@swi.psy.uva.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 1985-2002, University of Amsterdam

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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)(intptr_t 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(intptr_t size)			/* similar to sbrk() */
{ void *mem = top;

  if ( top + size >= atop )
  { void *ntop = (void *)roundup((uintptr_t) 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((uintptr_t) address, pagsize);
  top	     = base;
  atop	     = base;
  __morecore = mycore;
}

#ifdef TEST

#ifndef HAVE_STRTOUL
uintptr_t
strtoul(const char *s, char **eptr, int base)
{ static unsigned char xmap[256];
  static int xmap_initialised = 0;
  uintptr_t 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++)
  { uintptr_t 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;
  uintptr_t 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