(message-do-fcc): Modernise the code slightly.
[emacs.git] / src / sheap.c
blobf7028b0cf765a8fb4039dec2c134702b5b2d95f8
1 /* simulate `sbrk' with an array in .bss, for `unexec' support for Cygwin;
2 complete rewrite of xemacs Cygwin `unexec' code
4 Copyright (C) 2004-2017 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include "sheap.h"
25 #include <stdio.h>
26 #include "lisp.h"
27 #include <unistd.h>
28 #include <stdlib.h> /* for exit */
30 static int debug_sheap;
32 char bss_sbrk_buffer[STATIC_HEAP_SIZE];
33 char *max_bss_sbrk_ptr;
34 bool bss_sbrk_did_unexec;
36 void *
37 bss_sbrk (ptrdiff_t request_size)
39 static char *bss_sbrk_ptr;
41 if (!bss_sbrk_ptr)
43 max_bss_sbrk_ptr = bss_sbrk_ptr = bss_sbrk_buffer;
44 #ifdef CYGWIN
45 /* Force space for fork to work. */
46 sbrk (4096);
47 #endif
50 int used = bss_sbrk_ptr - bss_sbrk_buffer;
52 if (request_size < -used)
54 printf (("attempt to free too much: "
55 "avail %d used %d failed request %"pD"d\n"),
56 STATIC_HEAP_SIZE, used, request_size);
57 exit (-1);
58 return 0;
60 else if (STATIC_HEAP_SIZE - used < request_size)
62 printf ("static heap exhausted: avail %d used %d failed request %"pD"d\n",
63 STATIC_HEAP_SIZE, used, request_size);
64 exit (-1);
65 return 0;
68 void *ret = bss_sbrk_ptr;
69 bss_sbrk_ptr += request_size;
70 if (max_bss_sbrk_ptr < bss_sbrk_ptr)
71 max_bss_sbrk_ptr = bss_sbrk_ptr;
72 if (debug_sheap)
74 if (request_size < 0)
75 printf ("freed size %"pD"d\n", request_size);
76 else
77 printf ("allocated %p size %"pD"d\n", ret, request_size);
79 return ret;