sheap.c 2.06 KB
Newer Older
1 2
/* simulate `sbrk' with an array in .bss, for `unexec' support for Cygwin;
   complete rewrite of xemacs Cygwin `unexec' code
Stefan Monnier's avatar
Stefan Monnier committed
3

Paul Eggert's avatar
Paul Eggert committed
4
   Copyright (C) 2004-2019 Free Software Foundation, Inc.
Stefan Monnier's avatar
Stefan Monnier committed
5 6 7

This file is part of GNU Emacs.

8
GNU Emacs is free software: you can redistribute it and/or modify
Stefan Monnier's avatar
Stefan Monnier committed
9
it under the terms of the GNU General Public License as published by
10 11
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
Stefan Monnier's avatar
Stefan Monnier committed
12 13 14 15 16 17 18

GNU Emacs 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 General Public License for more details.

You should have received a copy of the GNU General Public License
19
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
Stefan Monnier's avatar
Stefan Monnier committed
20 21

#include <config.h>
22 23 24

#include "sheap.h"

Stefan Monnier's avatar
Stefan Monnier committed
25 26 27
#include <stdio.h>
#include "lisp.h"
#include <unistd.h>
Ken Brown's avatar
Ken Brown committed
28
#include <stdlib.h>		/* for exit */
Stefan Monnier's avatar
Stefan Monnier committed
29

30
static int debug_sheap;
Stefan Monnier's avatar
Stefan Monnier committed
31 32

char bss_sbrk_buffer[STATIC_HEAP_SIZE];
33
char *max_bss_sbrk_ptr;
Stefan Monnier's avatar
Stefan Monnier committed
34 35 36 37

void *
bss_sbrk (ptrdiff_t request_size)
{
38 39
  static char *bss_sbrk_ptr;

Stefan Monnier's avatar
Stefan Monnier committed
40 41
  if (!bss_sbrk_ptr)
    {
42
      max_bss_sbrk_ptr = bss_sbrk_ptr = bss_sbrk_buffer;
Stefan Monnier's avatar
Stefan Monnier committed
43
#ifdef CYGWIN
44 45
      /* Force space for fork to work.  */
      sbrk (4096);
Stefan Monnier's avatar
Stefan Monnier committed
46 47 48
#endif
    }

49 50 51
  int used = bss_sbrk_ptr - bss_sbrk_buffer;

  if (request_size < -used)
Stefan Monnier's avatar
Stefan Monnier committed
52
    {
53 54 55
      printf (("attempt to free too much: "
	       "avail %d used %d failed request %"pD"d\n"),
	      STATIC_HEAP_SIZE, used, request_size);
Stefan Monnier's avatar
Stefan Monnier committed
56 57 58
      exit (-1);
      return 0;
    }
59
  else if (STATIC_HEAP_SIZE - used < request_size)
Stefan Monnier's avatar
Stefan Monnier committed
60
    {
61 62
      printf ("static heap exhausted: avail %d used %d failed request %"pD"d\n",
	      STATIC_HEAP_SIZE, used, request_size);
Stefan Monnier's avatar
Stefan Monnier committed
63 64 65
      exit (-1);
      return 0;
    }
66 67 68 69 70 71

  void *ret = bss_sbrk_ptr;
  bss_sbrk_ptr += request_size;
  if (max_bss_sbrk_ptr < bss_sbrk_ptr)
    max_bss_sbrk_ptr = bss_sbrk_ptr;
  if (debug_sheap)
Stefan Monnier's avatar
Stefan Monnier committed
72
    {
73 74 75 76
      if (request_size < 0)
	printf ("freed size %"pD"d\n", request_size);
      else
	printf ("allocated %p size %"pD"d\n", ret, request_size);
Stefan Monnier's avatar
Stefan Monnier committed
77
    }
78
  return ret;
Stefan Monnier's avatar
Stefan Monnier committed
79
}