alloc.c 92.2 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Storage allocation and gc for GNU Emacs Lisp interpreter.
2
   Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 98, 1999, 2000
Richard M. Stallman's avatar
Richard M. Stallman committed
3
      Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
4 5 6 7 8

This file is part of GNU Emacs.

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

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
along with GNU Emacs; see the file COPYING.  If not, write to
19 20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Jim Blandy's avatar
Jim Blandy committed
21

22
#include <config.h>
23

24
/* Note that this declares bzero on OSF/1.  How dumb.  */
25

26
#include <signal.h>
27

28 29 30
/* This file is part of the core Lisp implementation, and thus must
   deal with the real data structures.  If the Lisp implementation is
   replaced, this file likely will not be used.  */
31

32
#undef HIDE_LISP_IMPLEMENTATION
Jim Blandy's avatar
Jim Blandy committed
33
#include "lisp.h"
34
#include "intervals.h"
Jim Blandy's avatar
Jim Blandy committed
35
#include "puresize.h"
Jim Blandy's avatar
Jim Blandy committed
36 37
#include "buffer.h"
#include "window.h"
Jim Blandy's avatar
Jim Blandy committed
38
#include "frame.h"
39
#include "blockinput.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
40
#include "keyboard.h"
41
#include "charset.h"
Jim Blandy's avatar
Jim Blandy committed
42 43
#include "syssignal.h"

Karl Heuer's avatar
Karl Heuer committed
44 45
extern char *sbrk ();

46
#ifdef DOUG_LEA_MALLOC
47

48 49
#include <malloc.h>
#define __malloc_size_t int
50

51 52 53
/* Specify maximum number of areas to mmap.  It would be nice to use a
   value that explicitly means "no limit".  */

54 55
#define MMAP_MAX_AREAS 100000000

56 57
#else /* not DOUG_LEA_MALLOC */

58 59 60 61 62 63 64 65 66 67
/* The following come from gmalloc.c.  */

#if defined (__STDC__) && __STDC__
#include <stddef.h>
#define	__malloc_size_t		size_t
#else
#define	__malloc_size_t		unsigned int
#endif
extern __malloc_size_t _bytes_used;
extern int __malloc_extra_blocks;
68 69

#endif /* not DOUG_LEA_MALLOC */
70

Jim Blandy's avatar
Jim Blandy committed
71
#define max(A,B) ((A) > (B) ? (A) : (B))
72
#define min(A,B) ((A) < (B) ? (A) : (B))
Jim Blandy's avatar
Jim Blandy committed
73 74 75 76 77

/* Macro to verify that storage intended for Lisp objects is not
   out of range to fit in the space for a pointer.
   ADDRESS is the start of the block, and SIZE
   is the amount of space within which objects can start.  */
78

Jim Blandy's avatar
Jim Blandy committed
79 80 81 82
#define VALIDATE_LISP_STORAGE(address, size)			\
do								\
  {								\
    Lisp_Object val;						\
83
    XSETCONS (val, (char *) address + size);		\
Jim Blandy's avatar
Jim Blandy committed
84 85
    if ((char *) XCONS (val) != (char *) address + size)	\
      {								\
86
	xfree (address);					\
Jim Blandy's avatar
Jim Blandy committed
87 88 89 90
	memory_full ();						\
      }								\
  } while (0)

91
/* Value of _bytes_used, when spare_memory was freed.  */
92

93 94
static __malloc_size_t bytes_used_when_full;

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
/* Mark, unmark, query mark bit of a Lisp string.  S must be a pointer
   to a struct Lisp_String.  */

#define MARK_STRING(S)		XMARK ((S)->size)
#define UNMARK_STRING(S)	XUNMARK ((S)->size)
#define STRING_MARKED_P(S)	XMARKBIT ((S)->size)

/* Value is the number of bytes/chars of S, a pointer to a struct
   Lisp_String.  This must be used instead of STRING_BYTES (S) or
   S->size during GC, because S->size contains the mark bit for
   strings.  */

#define GC_STRING_BYTES(S)	(STRING_BYTES (S) & ~MARKBIT)
#define GC_STRING_CHARS(S)	((S)->size & ~MARKBIT)

/* Number of bytes of consing done since the last gc.  */

Jim Blandy's avatar
Jim Blandy committed
112 113
int consing_since_gc;

114
/* Count the amount of consing of various sorts of space.  */
115

116 117 118 119 120 121 122
int cons_cells_consed;
int floats_consed;
int vector_cells_consed;
int symbols_consed;
int string_chars_consed;
int misc_objects_consed;
int intervals_consed;
123 124 125
int strings_consed;

/* Number of bytes of consing since GC before another GC should be done. */
126

127
int gc_cons_threshold;
Jim Blandy's avatar
Jim Blandy committed
128

129 130
/* Nonzero during GC.  */

Jim Blandy's avatar
Jim Blandy committed
131 132
int gc_in_progress;

133
/* Nonzero means display messages at beginning and end of GC.  */
134

135 136
int garbage_collection_messages;

Jim Blandy's avatar
Jim Blandy committed
137 138 139
#ifndef VIRT_ADDR_VARIES
extern
#endif /* VIRT_ADDR_VARIES */
140
int malloc_sbrk_used;
Jim Blandy's avatar
Jim Blandy committed
141 142 143 144

#ifndef VIRT_ADDR_VARIES
extern
#endif /* VIRT_ADDR_VARIES */
145
int malloc_sbrk_unused;
Jim Blandy's avatar
Jim Blandy committed
146

Jim Blandy's avatar
Jim Blandy committed
147
/* Two limits controlling how much undo information to keep.  */
148

Jim Blandy's avatar
Jim Blandy committed
149 150
int undo_limit;
int undo_strong_limit;
Jim Blandy's avatar
Jim Blandy committed
151

152
int total_conses, total_markers, total_symbols, total_vector_size;
153 154 155
int total_free_conses, total_free_markers, total_free_symbols;
int total_free_floats, total_floats;

156 157 158
/* Points to memory space allocated as "spare", to be freed if we run
   out of memory.  */

159 160 161
static char *spare_memory;

/* Amount of spare memory to keep in reserve.  */
162

163 164 165
#define SPARE_MEMORY (1 << 14)

/* Number of extra blocks malloc should get when it needs more core.  */
166

167 168
static int malloc_hysteresis;

169 170 171
/* Nonzero when malloc is called for allocating Lisp object space.
   Currently set but not used.  */

Karl Heuer's avatar
Karl Heuer committed
172 173
int allocating_for_lisp;

174 175
/* Non-nil means defun should do purecopy on the function definition.  */

Jim Blandy's avatar
Jim Blandy committed
176 177 178
Lisp_Object Vpurify_flag;

#ifndef HAVE_SHM
179 180 181 182

/* Force it into data space! */

EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {0,};
Jim Blandy's avatar
Jim Blandy committed
183
#define PUREBEG (char *) pure
184 185 186

#else /* not HAVE_SHM */

Jim Blandy's avatar
Jim Blandy committed
187 188
#define pure PURE_SEG_BITS   /* Use shared memory segment */
#define PUREBEG (char *)PURE_SEG_BITS
Jim Blandy's avatar
Jim Blandy committed
189 190 191

/* This variable is used only by the XPNTR macro when HAVE_SHM is
   defined.  If we used the PURESIZE macro directly there, that would
192
   make most of Emacs dependent on puresize.h, which we don't want -
Jim Blandy's avatar
Jim Blandy committed
193 194 195
   you should be able to change that without too much recompilation.
   So map_in_data initializes pure_size, and the dependencies work
   out.  */
196

197
EMACS_INT pure_size;
198

Jim Blandy's avatar
Jim Blandy committed
199 200
#endif /* not HAVE_SHM */

201 202
/* Index in pure at which next pure object will be allocated.. */

Jim Blandy's avatar
Jim Blandy committed
203 204
int pureptr;

205 206 207
/* If nonzero, this is a warning delivered by malloc and not yet
   displayed.  */

Jim Blandy's avatar
Jim Blandy committed
208 209
char *pending_malloc_warning;

210
/* Pre-computed signal argument for use when memory is exhausted.  */
211

212
Lisp_Object memory_signal_data;
213

Jim Blandy's avatar
Jim Blandy committed
214 215 216 217 218 219 220 221 222 223 224
/* Maximum amount of C stack to save when a GC happens.  */

#ifndef MAX_SAVE_STACK
#define MAX_SAVE_STACK 16000
#endif

/* Buffer in which we save a copy of the C stack at each GC.  */

char *stack_copy;
int stack_copy_size;

225 226 227
/* Non-zero means ignore malloc warnings.  Set during initialization.
   Currently not used.  */

Jim Blandy's avatar
Jim Blandy committed
228
int ignore_warnings;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
229

230
Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots;
231

232 233 234
static void mark_buffer P_ ((Lisp_Object));
static void mark_kboards P_ ((void));
static void gc_sweep P_ ((void));
235 236
static void mark_glyph_matrix P_ ((struct glyph_matrix *));
static void mark_face_cache P_ ((struct face_cache *));
237 238 239
#if 0
static void clear_marks ();
#endif
240 241 242 243 244 245

#ifdef HAVE_WINDOW_SYSTEM
static void mark_image P_ ((struct image *));
static void mark_image_cache P_ ((struct frame *));
#endif /* HAVE_WINDOW_SYSTEM */

246 247 248 249
static struct Lisp_String *allocate_string P_ ((void));
static void compact_small_strings P_ ((void));
static void free_large_strings P_ ((void));
static void sweep_strings P_ ((void));
Richard M. Stallman's avatar
Richard M. Stallman committed
250 251

extern int message_enable_multibyte;
Jim Blandy's avatar
Jim Blandy committed
252

253 254
/* Versions of malloc and realloc that print warnings as memory gets
   full.  */
255

Jim Blandy's avatar
Jim Blandy committed
256 257 258 259 260 261 262 263 264 265 266
Lisp_Object
malloc_warning_1 (str)
     Lisp_Object str;
{
  Fprinc (str, Vstandard_output);
  write_string ("\nKilling some buffers may delay running out of memory.\n", -1);
  write_string ("However, certainly by the time you receive the 95% warning,\n", -1);
  write_string ("you should clean up, kill this Emacs, and start a new one.", -1);
  return Qnil;
}

267
/* malloc calls this if it finds we are near exhausting storage.  */
268 269

void
Jim Blandy's avatar
Jim Blandy committed
270 271 272 273 274 275
malloc_warning (str)
     char *str;
{
  pending_malloc_warning = str;
}

276
void
Jim Blandy's avatar
Jim Blandy committed
277 278 279 280 281 282 283 284 285
display_malloc_warning ()
{
  register Lisp_Object val;

  val = build_string (pending_malloc_warning);
  pending_malloc_warning = 0;
  internal_with_output_to_temp_buffer (" *Danger*", malloc_warning_1, val);
}

286
#ifdef DOUG_LEA_MALLOC
287
#  define BYTES_USED (mallinfo ().arena)
288
#else
289
#  define BYTES_USED _bytes_used
290 291
#endif

292
/* Called if malloc returns zero.  */
293

294
void
Jim Blandy's avatar
Jim Blandy committed
295 296
memory_full ()
{
297
#ifndef SYSTEM_MALLOC
298
  bytes_used_when_full = BYTES_USED;
299 300 301 302 303 304 305 306 307
#endif

  /* The first time we get here, free the spare memory.  */
  if (spare_memory)
    {
      free (spare_memory);
      spare_memory = 0;
    }

308 309
  /* This used to call error, but if we've run out of memory, we could
     get infinite recursion trying to build the string.  */
310
  while (1)
311
    Fsignal (Qnil, memory_signal_data);
312 313 314 315 316 317 318
}

/* Called if we can't allocate relocatable space for a buffer.  */

void
buffer_memory_full ()
{
319 320 321 322 323 324
  /* If buffers use the relocating allocator, no need to free
     spare_memory, because we may have plenty of malloc space left
     that we could get, and if we don't, the malloc that fails will
     itself cause spare_memory to be freed.  If buffers don't use the
     relocating allocator, treat this like any other failing
     malloc.  */
325 326 327 328 329

#ifndef REL_ALLOC
  memory_full ();
#endif

330 331
  /* This used to call error, but if we've run out of memory, we could
     get infinite recursion trying to build the string.  */
332 333
  while (1)
    Fsignal (Qerror, memory_signal_data);
Jim Blandy's avatar
Jim Blandy committed
334 335
}

336 337
/* Like malloc routines but check for no memory and block interrupt
   input..  */
Jim Blandy's avatar
Jim Blandy committed
338 339 340 341 342 343 344

long *
xmalloc (size)
     int size;
{
  register long *val;

345
  BLOCK_INPUT;
Jim Blandy's avatar
Jim Blandy committed
346
  val = (long *) malloc (size);
347
  UNBLOCK_INPUT;
Jim Blandy's avatar
Jim Blandy committed
348

349 350
  if (!val && size)
    memory_full ();
Jim Blandy's avatar
Jim Blandy committed
351 352 353 354 355 356 357 358 359 360
  return val;
}

long *
xrealloc (block, size)
     long *block;
     int size;
{
  register long *val;

361
  BLOCK_INPUT;
Jim Blandy's avatar
Jim Blandy committed
362 363 364 365
  /* We must call malloc explicitly when BLOCK is 0, since some
     reallocs don't do this.  */
  if (! block)
    val = (long *) malloc (size);
Noah Friedman's avatar
Noah Friedman committed
366
  else
Jim Blandy's avatar
Jim Blandy committed
367
    val = (long *) realloc (block, size);
368
  UNBLOCK_INPUT;
Jim Blandy's avatar
Jim Blandy committed
369 370 371 372

  if (!val && size) memory_full ();
  return val;
}
373 374 375 376 377 378 379 380 381 382

void
xfree (block)
     long *block;
{
  BLOCK_INPUT;
  free (block);
  UNBLOCK_INPUT;
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
/* Like malloc but used for allocating Lisp data.  */

long *
lisp_malloc (size)
     int size;
{
  register long *val;

  BLOCK_INPUT;
  allocating_for_lisp++;
  val = (long *) malloc (size);
  allocating_for_lisp--;
  UNBLOCK_INPUT;

  if (!val && size) memory_full ();
  return val;
}

void
lisp_free (block)
     long *block;
{
  BLOCK_INPUT;
  allocating_for_lisp++;
  free (block);
  allocating_for_lisp--;
  UNBLOCK_INPUT;
}
411 412 413 414 415 416 417 418 419 420 421 422

/* Arranging to disable input signals while we're in malloc.

   This only works with GNU malloc.  To help out systems which can't
   use GNU malloc, all the calls to malloc, realloc, and free
   elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
   pairs; unfortunately, we have no idea what C library functions
   might call malloc, so we can't really protect them unless you're
   using GNU malloc.  Fortunately, most of the major operating can use
   GNU malloc.  */

#ifndef SYSTEM_MALLOC
423

424 425 426 427 428 429
extern void * (*__malloc_hook) ();
static void * (*old_malloc_hook) ();
extern void * (*__realloc_hook) ();
static void * (*old_realloc_hook) ();
extern void (*__free_hook) ();
static void (*old_free_hook) ();
430

431 432
/* This function is used as the hook for free to call.  */

433 434 435 436 437 438 439
static void
emacs_blocked_free (ptr)
     void *ptr;
{
  BLOCK_INPUT;
  __free_hook = old_free_hook;
  free (ptr);
440 441 442 443 444 445 446 447 448
  /* If we released our reserve (due to running out of memory),
     and we have a fair amount free once again,
     try to set aside another reserve in case we run out once more.  */
  if (spare_memory == 0
      /* Verify there is enough space that even with the malloc
	 hysteresis this call won't run out again.
	 The code here is correct as long as SPARE_MEMORY
	 is substantially larger than the block size malloc uses.  */
      && (bytes_used_when_full
449
	  > BYTES_USED + max (malloc_hysteresis, 4) * SPARE_MEMORY))
450 451
    spare_memory = (char *) malloc (SPARE_MEMORY);

452
  __free_hook = emacs_blocked_free;
453 454 455
  UNBLOCK_INPUT;
}

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
/* If we released our reserve (due to running out of memory),
   and we have a fair amount free once again,
   try to set aside another reserve in case we run out once more.

   This is called when a relocatable block is freed in ralloc.c.  */

void
refill_memory_reserve ()
{
  if (spare_memory == 0)
    spare_memory = (char *) malloc (SPARE_MEMORY);
}

/* This function is the malloc hook that Emacs uses.  */

471 472 473 474 475 476 477 478
static void *
emacs_blocked_malloc (size)
     unsigned size;
{
  void *value;

  BLOCK_INPUT;
  __malloc_hook = old_malloc_hook;
479
#ifdef DOUG_LEA_MALLOC
480
    mallopt (M_TOP_PAD, malloc_hysteresis * 4096);
481
#else
482
    __malloc_extra_blocks = malloc_hysteresis;
483
#endif
484
  value = (void *) malloc (size);
485
  __malloc_hook = emacs_blocked_malloc;
486 487 488 489 490 491 492 493 494 495 496 497 498 499
  UNBLOCK_INPUT;

  return value;
}

static void *
emacs_blocked_realloc (ptr, size)
     void *ptr;
     unsigned size;
{
  void *value;

  BLOCK_INPUT;
  __realloc_hook = old_realloc_hook;
500
  value = (void *) realloc (ptr, size);
501
  __realloc_hook = emacs_blocked_realloc;
502 503 504 505 506 507 508 509
  UNBLOCK_INPUT;

  return value;
}

void
uninterrupt_malloc ()
{
510 511
  if (__free_hook != emacs_blocked_free)
    old_free_hook = __free_hook;
512
  __free_hook = emacs_blocked_free;
513

514 515
  if (__malloc_hook != emacs_blocked_malloc)
    old_malloc_hook = __malloc_hook;
516
  __malloc_hook = emacs_blocked_malloc;
517

518 519
  if (__realloc_hook != emacs_blocked_realloc)
    old_realloc_hook = __realloc_hook;
520
  __realloc_hook = emacs_blocked_realloc;
521
}
522 523 524 525

#endif /* not SYSTEM_MALLOC */


Jim Blandy's avatar
Jim Blandy committed
526

527 528 529
/***********************************************************************
			 Interval Allocation
 ***********************************************************************/
530

531 532 533 534
#define INTERVAL_BLOCK_SIZE \
  ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))

struct interval_block
535 536 537 538
{
  struct interval_block *next;
  struct interval intervals[INTERVAL_BLOCK_SIZE];
};
539 540 541

struct interval_block *interval_block;
static int interval_block_index;
542
static int total_free_intervals, total_intervals;
543 544 545

INTERVAL interval_free_list;

546
/* Total number of interval blocks now in use.  */
547

548 549
int n_interval_blocks;

550 551 552 553
static void
init_intervals ()
{
  interval_block
554
    = (struct interval_block *) lisp_malloc (sizeof (struct interval_block));
555
  interval_block->next = 0;
Richard M. Stallman's avatar
Richard M. Stallman committed
556
  bzero ((char *) interval_block->intervals, sizeof interval_block->intervals);
557 558
  interval_block_index = 0;
  interval_free_list = 0;
559
  n_interval_blocks = 1;
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
}

#define INIT_INTERVALS init_intervals ()

INTERVAL
make_interval ()
{
  INTERVAL val;

  if (interval_free_list)
    {
      val = interval_free_list;
      interval_free_list = interval_free_list->parent;
    }
  else
    {
      if (interval_block_index == INTERVAL_BLOCK_SIZE)
	{
Karl Heuer's avatar
Karl Heuer committed
578 579
	  register struct interval_block *newi;

580
	  newi = (struct interval_block *) lisp_malloc (sizeof (struct interval_block));
581 582 583 584 585

	  VALIDATE_LISP_STORAGE (newi, sizeof *newi);
	  newi->next = interval_block;
	  interval_block = newi;
	  interval_block_index = 0;
586
	  n_interval_blocks++;
587 588 589 590
	}
      val = &interval_block->intervals[interval_block_index++];
    }
  consing_since_gc += sizeof (struct interval);
591
  intervals_consed++;
592 593 594 595 596 597 598
  RESET_INTERVAL (val);
  return val;
}

/* Mark the pointers of one interval. */

static void
599
mark_interval (i, dummy)
600
     register INTERVAL i;
601
     Lisp_Object dummy;
602 603 604 605 606 607 608 609 610 611 612
{
  if (XMARKBIT (i->plist))
    abort ();
  mark_object (&i->plist);
  XMARK (i->plist);
}

static void
mark_interval_tree (tree)
     register INTERVAL tree;
{
613 614 615 616 617 618 619
  /* No need to test if this tree has been marked already; this
     function is always called through the MARK_INTERVAL_TREE macro,
     which takes care of that.  */

  /* XMARK expands to an assignment; the LHS of an assignment can't be
     a cast.  */
  XMARK (* (Lisp_Object *) &tree->parent);
620

621
  traverse_intervals (tree, 1, 0, mark_interval, Qnil);
622 623
}

624 625 626
#define MARK_INTERVAL_TREE(i)				\
  do {							\
    if (!NULL_INTERVAL_P (i)				\
627
	&& ! XMARKBIT (*(Lisp_Object *) &i->parent))	\
628 629
      mark_interval_tree (i);				\
  } while (0)
630

631
/* The oddity in the call to XUNMARK is necessary because XUNMARK
632 633 634 635 636 637 638 639 640 641 642
   expands to an assignment to its argument, and most C compilers
   don't support casts on the left operand of `='.  */

#define UNMARK_BALANCE_INTERVALS(i)			\
  do {							\
   if (! NULL_INTERVAL_P (i))				\
     {							\
       XUNMARK (* (Lisp_Object *) (&(i)->parent));	\
       (i) = balance_intervals (i);			\
     }							\
  } while (0)
643 644


645 646 647
/***********************************************************************
			  String Allocation
 ***********************************************************************/
648

649 650 651 652 653 654
/* Lisp_Strings are allocated in string_block structures.  When a new
   string_block is allocated, all the Lisp_Strings it contains are
   added to a free-list stiing_free_list.  When a new Lisp_String is
   needed, it is taken from that list.  During the sweep phase of GC,
   string_blocks that are entirely free are freed, except two which
   we keep.
Jim Blandy's avatar
Jim Blandy committed
655

656 657 658
   String data is allocated from sblock structures.  Strings larger
   than LARGE_STRING_BYTES, get their own sblock, data for smaller
   strings is sub-allocated out of sblocks of size SBLOCK_SIZE.
Jim Blandy's avatar
Jim Blandy committed
659

660 661 662 663
   Sblocks consist internally of sdata structures, one for each
   Lisp_String.  The sdata structure points to the Lisp_String it
   belongs to.  The Lisp_String points back to the `u.data' member of
   its sdata structure.
Jim Blandy's avatar
Jim Blandy committed
664

665 666 667 668 669 670
   When a Lisp_String is freed during GC, it is put back on
   string_free_list, and its `data' member and its sdata's `string'
   pointer is set to null.  The size of the string is recorded in the
   `u.nbytes' member of the sdata.  So, sdata structures that are no
   longer used, can be easily recognized, and it's easy to compact the
   sblocks of small strings which we do in compact_small_strings.  */
Jim Blandy's avatar
Jim Blandy committed
671

672 673
/* Size in bytes of an sblock structure used for small strings.  This
   is 8192 minus malloc overhead.  */
Jim Blandy's avatar
Jim Blandy committed
674

675
#define SBLOCK_SIZE 8188
676

677 678
/* Strings larger than this are considered large strings.  String data
   for large strings is allocated from individual sblocks.  */
Jim Blandy's avatar
Jim Blandy committed
679

680 681 682 683 684 685
#define LARGE_STRING_BYTES 1024

/* Structure describing string memory sub-allocated from an sblock.
   This is where the contents of Lisp strings are stored.  */

struct sdata
Jim Blandy's avatar
Jim Blandy committed
686
{
687 688 689 690 691 692 693
  /* Back-pointer to the string this sdata belongs to.  If null, this
     structure is free, and the NBYTES member of the union below
     contains the string byte size (the same value that STRING_BYTES
     would return if STRING were non-null).  If non-null, STRING_BYTES
     (STRING) is the size of the data, and DATA contains the string's
     contents.  */
  struct Lisp_String *string;
Jim Blandy's avatar
Jim Blandy committed
694

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
  union
  {
    /* When STRING in non-null.  */
    unsigned char data[1];

    /* When STRING is null.  */
    EMACS_INT nbytes;
  } u;
};

/* Structure describing a block of memory which is sub-allocated to
   obtain string data memory for strings.  Blocks for small strings
   are of fixed size SBLOCK_SIZE.  Blocks for large strings are made
   as large as needed.  */

struct sblock
Jim Blandy's avatar
Jim Blandy committed
711
{
712 713
  /* Next in list.  */
  struct sblock *next;
Jim Blandy's avatar
Jim Blandy committed
714

715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
  /* Pointer to the next free sdata block.  This points past the end
     of the sblock if there isn't any space left in this block.  */
  struct sdata *next_free;

  /* Start of data.  */
  struct sdata first_data;
};

/* Number of Lisp strings in a string_block structure.  The 1020 is
   1024 minus malloc overhead.  */

#define STRINGS_IN_STRING_BLOCK \
  ((1020 - sizeof (struct string_block *)) / sizeof (struct Lisp_String))

/* Structure describing a block from which Lisp_String structures
   are allocated.  */

struct string_block
Jim Blandy's avatar
Jim Blandy committed
733
{
734 735 736
  struct string_block *next;
  struct Lisp_String strings[STRINGS_IN_STRING_BLOCK];
};
Jim Blandy's avatar
Jim Blandy committed
737

738 739 740
/* Head and tail of the list of sblock structures holding Lisp string
   data.  We always allocate from current_sblock.  The NEXT pointers
   in the sblock structures go from oldest_sblock to current_sblock.  */
Karl Heuer's avatar
Karl Heuer committed
741

742
static struct sblock *oldest_sblock, *current_sblock;
Jim Blandy's avatar
Jim Blandy committed
743

744
/* List of sblocks for large strings.  */
Jim Blandy's avatar
Jim Blandy committed
745

746
static struct sblock *large_sblocks;
Jim Blandy's avatar
Jim Blandy committed
747

748
/* List of string_block structures, and how many there are.  */
Jim Blandy's avatar
Jim Blandy committed
749

750 751
static struct string_block *string_blocks;
static int n_string_blocks;
Jim Blandy's avatar
Jim Blandy committed
752

753
/* Free-list of Lisp_Strings.  */
Jim Blandy's avatar
Jim Blandy committed
754

755
static struct Lisp_String *string_free_list;
Jim Blandy's avatar
Jim Blandy committed
756

757
/* Number of live and free Lisp_Strings.  */
758

759
static int total_strings, total_free_strings;
Jim Blandy's avatar
Jim Blandy committed
760

761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
/* Number of bytes used by live strings.  */

static int total_string_size;

/* Given a pointer to a Lisp_String S which is on the free-list
   string_free_list, return a pointer to its successor in the
   free-list.  */

#define NEXT_FREE_LISP_STRING(S) (*(struct Lisp_String **) (S))

/* Return a pointer to the sdata structure belonging to Lisp string S.
   S must be live, i.e. S->data must not be null.  S->data is actually
   a pointer to the `u.data' member of its sdata structure; the
   structure starts at a constant offset in front of that.  */
   
#define SDATA_OF_STRING(S) \
     ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *)))

/* Value is the size of an sdata structure large enough to hold NBYTES
   bytes of string data.  The value returned includes a terminating
   NUL byte, the size of the sdata structure, and padding.  */

#define SDATA_SIZE(NBYTES)			\
     ((sizeof (struct Lisp_String *)		\
       + (NBYTES) + 1				\
       + sizeof (EMACS_INT) - 1)		\
      & ~(sizeof (EMACS_INT) - 1))


/* Initialize string allocation.  Called from init_alloc_once.  */
791 792

void
793
init_strings ()
Jim Blandy's avatar
Jim Blandy committed
794
{
795 796 797 798 799
  total_strings = total_free_strings = total_string_size = 0;
  oldest_sblock = current_sblock = large_sblocks = NULL;
  string_blocks = NULL;
  n_string_blocks = 0;
  string_free_list = NULL;
Jim Blandy's avatar
Jim Blandy committed
800 801
}

802 803 804 805 806

/* Return a new Lisp_String.  */

static struct Lisp_String *
allocate_string ()
Jim Blandy's avatar
Jim Blandy committed
807
{
808
  struct Lisp_String *s;
Jim Blandy's avatar
Jim Blandy committed
809

810 811 812
  /* If the free-list is empty, allocate a new string_block, and
     add all the Lisp_Strings in it to the free-list.  */
  if (string_free_list == NULL)
Jim Blandy's avatar
Jim Blandy committed
813
    {
814 815 816 817 818 819 820 821 822 823 824
      struct string_block *b;
      int i;

      b = (struct string_block *) lisp_malloc (sizeof *b);
      VALIDATE_LISP_STORAGE (b, sizeof *b);
      bzero (b, sizeof *b);
      b->next = string_blocks;
      string_blocks = b;
      ++n_string_blocks;

      for (i = STRINGS_IN_STRING_BLOCK - 1; i >= 0; --i)
Jim Blandy's avatar
Jim Blandy committed
825
	{
826 827 828
	  s = b->strings + i;
	  NEXT_FREE_LISP_STRING (s) = string_free_list;
	  string_free_list = s;
Jim Blandy's avatar
Jim Blandy committed
829
	}
830 831

      total_free_strings += STRINGS_IN_STRING_BLOCK;
Jim Blandy's avatar
Jim Blandy committed
832
    }
833

834 835 836
  /* Pop a Lisp_String off the free-list.  */
  s = string_free_list;
  string_free_list = NEXT_FREE_LISP_STRING (s);
837

838 839
  /* Probably not strictly necessary, but play it safe.  */
  bzero (s, sizeof *s);
840

841 842 843 844
  --total_free_strings;
  ++total_strings;
  ++strings_consed;
  consing_since_gc += sizeof *s;
845

846
  return s;
847
}
Jim Blandy's avatar
Jim Blandy committed
848 849


850 851 852 853 854
/* Set up Lisp_String S for holding NCHARS characters, NBYTES bytes,
   plus a NUL byte at the end.  Allocate an sdata structure for S, and
   set S->data to its `u.data' member.  Store a NUL byte at the end of
   S->data.  Set S->size to NCHARS and S->size_byte to NBYTES.  Free
   S->data if it was initially non-null.  */
Jim Blandy's avatar
Jim Blandy committed
855

856 857 858 859
void
allocate_string_data (s, nchars, nbytes)
     struct Lisp_String *s;
     int nchars, nbytes;
Jim Blandy's avatar
Jim Blandy committed
860
{
861 862 863
  struct sdata *data;
  struct sblock *b;
  int needed;
Jim Blandy's avatar
Jim Blandy committed
864

865 866 867
  /* Determine the number of bytes needed to store NBYTES bytes
     of string data.  */
  needed = SDATA_SIZE (nbytes);
Jim Blandy's avatar
Jim Blandy committed
868

869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439