root/src/sheap.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. bss_sbrk

     1 /* simulate `sbrk' with an array in .bss, for `unexec' support for Cygwin;
     2    complete rewrite of xemacs Cygwin `unexec' code
     3 
     4    Copyright (C) 2004-2023 Free Software Foundation, Inc.
     5 
     6 This file is part of GNU Emacs.
     7 
     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.
    12 
    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.
    17 
    18 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/>.  */
    20 
    21 #include <config.h>
    22 
    23 #include "sheap.h"
    24 
    25 #include <stdio.h>
    26 #include "lisp.h"
    27 #include <unistd.h>
    28 #include <stdlib.h>             /* for exit */
    29 
    30 static int debug_sheap;
    31 
    32 char bss_sbrk_buffer[STATIC_HEAP_SIZE];
    33 char *max_bss_sbrk_ptr;
    34 
    35 void *
    36 bss_sbrk (ptrdiff_t request_size)
    37 {
    38   static char *bss_sbrk_ptr;
    39 
    40   if (!bss_sbrk_ptr)
    41     {
    42       max_bss_sbrk_ptr = bss_sbrk_ptr = bss_sbrk_buffer;
    43 #ifdef CYGWIN
    44       /* Force space for fork to work.  */
    45       sbrk (4096);
    46 #endif
    47     }
    48 
    49   int used = bss_sbrk_ptr - bss_sbrk_buffer;
    50 
    51   if (request_size < -used)
    52     {
    53       printf (("attempt to free too much: "
    54                "avail %d used %d failed request %"pD"d\n"),
    55               STATIC_HEAP_SIZE, used, request_size);
    56       exit (-1);
    57       return 0;
    58     }
    59   else if (STATIC_HEAP_SIZE - used < request_size)
    60     {
    61       printf ("static heap exhausted: avail %d used %d failed request %"pD"d\n",
    62               STATIC_HEAP_SIZE, used, request_size);
    63       exit (-1);
    64       return 0;
    65     }
    66 
    67   void *ret = bss_sbrk_ptr;
    68   bss_sbrk_ptr += request_size;
    69   if (max_bss_sbrk_ptr < bss_sbrk_ptr)
    70     max_bss_sbrk_ptr = bss_sbrk_ptr;
    71   if (debug_sheap)
    72     {
    73       if (request_size < 0)
    74         printf ("freed size %"pD"d\n", request_size);
    75       else
    76         printf ("allocated %p size %"pD"d\n", ret, request_size);
    77     }
    78   return ret;
    79 }

/* [<][>][^][v][top][bottom][index][help] */