home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / stdlib / MemChunkAlloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-27  |  2.7 KB  |  87 lines

  1. /* 
  2.  * MemChunkAlloc.c --
  3.  *
  4.  *    Source code for the "MemChunkAlloc" procedure, which is used
  5.  *    internally by the memory allocator to create new memory for
  6.  *    user-level programs.  Different programs or uses of the
  7.  *    allocator may replace this version of MemChunkAlloc with
  8.  *    something appropriate to the particular program.  See memInt.h
  9.  *    for overall information about how the allocator works.
  10.  *
  11.  * Copyright 1985, 1988 Regents of the University of California
  12.  * Permission to use, copy, modify, and distribute this
  13.  * software and its documentation for any purpose and without
  14.  * fee is hereby granted, provided that the above copyright
  15.  * notice appear in all copies.  The University of California
  16.  * makes no representations about the suitability of this
  17.  * software for any purpose.  It is provided "as is" without
  18.  * express or implied warranty.
  19.  */
  20.  
  21. #ifndef lint
  22. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/MemChunkAlloc.c,v 1.4 90/09/27 04:42:29 rab Exp $ SPRITE (Berkeley)";
  23. #endif not lint
  24.  
  25. #include "memInt.h"
  26. #include <stdio.h>
  27. #include <sys/types.h>
  28.  
  29. /*
  30.  * UNIX library imports:
  31.  */
  32.  
  33. extern caddr_t    sbrk();
  34.  
  35. /*
  36.  * The variables below don't exactly belong in this file, but they
  37.  * need to be someplace that's use-dependent (just like MemChunkAlloc)
  38.  * so that they can be initialized differently for different uses
  39.  * of the allocator (e.g. kernel vs. user).
  40.  */
  41.  
  42. extern int    fprintf();
  43. int        (*memPrintProc)()    = fprintf;
  44. ClientData    memPrintData        = (ClientData) stdout;
  45.  
  46. /*
  47.  *----------------------------------------------------------------------
  48.  *
  49.  * MemChunkAlloc --
  50.  *
  51.  *    malloc calls this procedure to get another region of storage
  52.  *    from the system (i.e. whenever the storage it's gotten so far
  53.  *    is insufficient to meet a request).  The actual size returned 
  54.  *    may be greater than size but not less.  This region now becomes 
  55.  *    the permanent property of malloc, and will never be returned.  
  56.  *
  57.  * Results:
  58.  *    The return value is a pointer to a new block of memory that
  59.  *    is size bytes long.
  60.  *
  61.  * Side effects:
  62.  *    The virtual address space of the process is extended.
  63.  *    If the VAS can't be increased, the process is terminated.
  64.  *
  65.  *----------------------------------------------------------------------
  66.  */
  67.  
  68. Address
  69. MemChunkAlloc(size)
  70.     int size;            /* Number of bytes desired.  */
  71. {
  72.     Address result;
  73.     int misAlignment;
  74.  
  75.     result = (Address) sbrk(size);
  76.     if (result == (Address) -1) {
  77.     panic("MemChunkAlloc couldn't extend heap");
  78.     return(0);        /* should never get here */
  79.     }
  80.     /* Make sure `result' is aligned to hold at least a double */
  81.     if ((misAlignment = (int) result & 7) != 0) {
  82.     result += 8 - misAlignment;
  83.     (Address) sbrk(8 - misAlignment);
  84.     }
  85.     return result;
  86. }
  87.