home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / os / bsdss4.tz / bsdss4 / bsdss / server / sys / malloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-22  |  9.5 KB  |  270 lines

  1. /* 
  2.  * Mach Operating System
  3.  * Copyright (c) 1992 Carnegie Mellon University
  4.  * All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify and distribute this software and its
  7.  * documentation is hereby granted, provided that both the copyright
  8.  * notice and this permission notice appear in all copies of the
  9.  * software, derivative works or modified versions, and any portions
  10.  * thereof, and that both notices appear in supporting documentation.
  11.  * 
  12.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15.  * 
  16.  * Carnegie Mellon requests users of this software to return to
  17.  * 
  18.  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  19.  *  School of Computer Science
  20.  *  Carnegie Mellon University
  21.  *  Pittsburgh PA 15213-3890
  22.  * 
  23.  * any improvements or extensions that they make and grant Carnegie Mellon 
  24.  * the rights to redistribute these changes.
  25.  */
  26. /*
  27.  * HISTORY
  28.  * $Log:    malloc.h,v $
  29.  * Revision 2.1  92/04/21  17:15:39  rwd
  30.  * BSDSS
  31.  * 
  32.  *
  33.  */
  34.  
  35. /*
  36.  * Copyright (c) 1987 Regents of the University of California.
  37.  * All rights reserved.
  38.  *
  39.  * Redistribution and use in source and binary forms, with or without
  40.  * modification, are permitted provided that the following conditions
  41.  * are met:
  42.  * 1. Redistributions of source code must retain the above copyright
  43.  *    notice, this list of conditions and the following disclaimer.
  44.  * 2. Redistributions in binary form must reproduce the above copyright
  45.  *    notice, this list of conditions and the following disclaimer in the
  46.  *    documentation and/or other materials provided with the distribution.
  47.  * 3. All advertising materials mentioning features or use of this software
  48.  *    must display the following acknowledgement:
  49.  *    This product includes software developed by the University of
  50.  *    California, Berkeley and its contributors.
  51.  * 4. Neither the name of the University nor the names of its contributors
  52.  *    may be used to endorse or promote products derived from this software
  53.  *    without specific prior written permission.
  54.  *
  55.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  56.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  58.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  59.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  60.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  61.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  62.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  63.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  64.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  65.  * SUCH DAMAGE.
  66.  *
  67.  *    @(#)malloc.h    7.25 (Berkeley) 5/15/91
  68.  */
  69.  
  70. #include <kmemstats.h>
  71. #ifndef _MALLOC_H_
  72. #define    _MALLOC_H_
  73.  
  74. /*
  75.  * flags to malloc
  76.  */
  77. #define    M_WAITOK    0x0000
  78. #define    M_NOWAIT    0x0001
  79.  
  80. /*
  81.  * Types of memory to be allocated
  82.  */
  83. #define    M_FREE        0    /* should be on free list */
  84. #define    M_MBUF        1    /* mbuf */
  85. #define    M_DEVBUF    2    /* device driver memory */
  86. #define    M_SOCKET    3    /* socket structure */
  87. #define    M_PCB        4    /* protocol control block */
  88. #define    M_RTABLE    5    /* routing tables */
  89. #define    M_HTABLE    6    /* IMP host tables */
  90. #define    M_FTABLE    7    /* fragment reassembly header */
  91. #define    M_ZOMBIE    8    /* zombie proc status */
  92. #define    M_IFADDR    9    /* interface address */
  93. #define    M_SOOPTS    10    /* socket options */
  94. #define    M_SONAME    11    /* socket name */
  95. #define    M_NAMEI        12    /* namei path name buffer */
  96. #define    M_GPROF        13    /* kernel profiling buffer */
  97. #define    M_IOCTLOPS    14    /* ioctl data buffer */
  98. #define    M_SUPERBLK    15    /* super block data */
  99. #define    M_CRED        16    /* credentials */
  100. #define    M_PGRP        17    /* process group header */
  101. #define    M_SESSION    18    /* session header */
  102. #define    M_IOV        19    /* large iov's */
  103. #define    M_MOUNT        20    /* vfs mount struct */
  104. #define    M_FHANDLE    21    /* network file handle */
  105. #define    M_NFSREQ    22    /* NFS request header */
  106. #define    M_NFSMNT    23    /* NFS mount structure */
  107. #define    M_VNODE        24    /* Dynamically allocated vnodes */
  108. #define    M_CACHE        25    /* Dynamically allocated cache entries */
  109. #define    M_DQUOT        26    /* UFS quota entries */
  110. #define    M_UFSMNT    27    /* UFS mount structure */
  111. #define    M_MAPMEM    28    /* mapped memory descriptors */
  112. #define    M_SHM        29    /* SVID compatible shared memory segments */
  113. #define    M_VMMAP        30    /* VM map structures */
  114. #define    M_VMMAPENT    31    /* VM map entry structures */
  115. #define    M_VMOBJ        32    /* VM object structure */
  116. #define    M_VMOBJHASH    33    /* VM object hash structure */
  117. #define    M_VMPMAP    34    /* VM pmap */
  118. #define    M_VMPVENT    35    /* VM phys-virt mapping entry */
  119. #define    M_VMPAGER    36    /* XXX: VM pager struct */
  120. #define    M_VMPGDATA    37    /* XXX: VM pager private data */
  121. #define    M_FILE        38    /* Open file structure */
  122. #define    M_FILEDESC    39    /* Open file descriptor table */
  123. #define    M_LOCKF        40    /* Byte-range locking structures */
  124. #define    M_PROC        41    /* Proc structures */
  125. #define    M_SUBPROC    42    /* Proc sub-structures */
  126. #define    M_TEMP        49    /* misc temporary data buffers */
  127. #define    M_LAST        50
  128.  
  129. #define INITKMEMNAMES { \
  130.     "free",        /* 0 M_FREE */ \
  131.     "mbuf",        /* 1 M_MBUF */ \
  132.     "devbuf",    /* 2 M_DEVBUF */ \
  133.     "socket",    /* 3 M_SOCKET */ \
  134.     "pcb",        /* 4 M_PCB */ \
  135.     "routetbl",    /* 5 M_RTABLE */ \
  136.     "hosttbl",    /* 6 M_HTABLE */ \
  137.     "fragtbl",    /* 7 M_FTABLE */ \
  138.     "zombie",    /* 8 M_ZOMBIE */ \
  139.     "ifaddr",    /* 9 M_IFADDR */ \
  140.     "soopts",    /* 10 M_SOOPTS */ \
  141.     "soname",    /* 11 M_SONAME */ \
  142.     "namei",    /* 12 M_NAMEI */ \
  143.     "gprof",    /* 13 M_GPROF */ \
  144.     "ioctlops",    /* 14 M_IOCTLOPS */ \
  145.     "superblk",    /* 15 M_SUPERBLK */ \
  146.     "cred",        /* 16 M_CRED */ \
  147.     "pgrp",        /* 17 M_PGRP */ \
  148.     "session",    /* 18 M_SESSION */ \
  149.     "iov",        /* 19 M_IOV */ \
  150.     "mount",    /* 20 M_MOUNT */ \
  151.     "fhandle",    /* 21 M_FHANDLE */ \
  152.     "NFS req",    /* 22 M_NFSREQ */ \
  153.     "NFS mount",    /* 23 M_NFSMNT */ \
  154.     "vnodes",    /* 24 M_VNODE */ \
  155.     "namecache",    /* 25 M_CACHE */ \
  156.     "UFS quota",    /* 26 M_DQUOT */ \
  157.     "UFS mount",    /* 27 M_UFSMNT */ \
  158.     "mapmem",    /* 28 M_MAPMEM */ \
  159.     "shm",        /* 29 M_SHM */ \
  160.     "VM map",    /* 30 M_VMMAP */ \
  161.     "VM mapent",    /* 31 M_VMMAPENT */ \
  162.     "VM object",    /* 32 M_VMOBJ */ \
  163.     "VM objhash",    /* 33 M_VMOBJHASH */ \
  164.     "VM pmap",    /* 34 M_VMPMAP */ \
  165.     "VM pvmap",    /* 35 M_VMPVENT */ \
  166.     "VM pager",    /* 36 M_VMPAGER */ \
  167.     "VM pgdata",    /* 37 M_VMPGDATA */ \
  168.     "file",        /* 38 M_FILE */ \
  169.     "file desc",    /* 39 M_FILEDESC */ \
  170.     "lockf",    /* 40 M_LOCKF */ \
  171.     "proc",        /* 41 M_PROC */ \
  172.     "subproc",    /* 42 M_PROC */ \
  173.     0, 0, 0, 0, 0, 0, \
  174.     "temp",        /* 49 M_TEMP */ \
  175. }
  176.  
  177. struct kmemstats {
  178.     long    ks_inuse;    /* # of packets of this type currently in use */
  179.     long    ks_calls;    /* total packets of this type ever allocated */
  180.     long     ks_memuse;    /* total memory held in bytes */
  181.     u_short    ks_limblocks;    /* number of times blocked for hitting limit */
  182.     u_short    ks_mapblocks;    /* number of times blocked for kernel map */
  183.     long    ks_maxused;    /* maximum number ever used */
  184.     long    ks_limit;    /* most that are allowed to exist */
  185. };
  186.  
  187. /*
  188.  * Array of descriptors that describe the contents of each page
  189.  */
  190. struct kmemusage {
  191.     short ku_indx;        /* bucket index */
  192.     union {
  193.         u_short freecnt;/* for small allocations, free pieces in page */
  194.         u_short pagecnt;/* for large allocations, pages alloced */
  195.     } ku_un;
  196. };
  197. #define ku_freecnt ku_un.freecnt
  198. #define ku_pagecnt ku_un.pagecnt
  199.  
  200. /*
  201.  * Set of buckets for each size of memory block that is retained
  202.  */
  203. struct kmembuckets {
  204.     caddr_t kb_next;    /* list of free blocks */
  205.     long    kb_calls;    /* total calls to allocate this size */
  206.     long    kb_total;    /* total number of blocks allocated */
  207.     long    kb_totalfree;    /* # of free elements in this bucket */
  208.     long    kb_elmpercl;    /* # of elements in this sized allocation */
  209.     long    kb_highwat;    /* high water mark */
  210.     long    kb_couldfree;    /* over high water mark and could free */
  211. };
  212.  
  213. #ifdef KERNEL
  214. #define    MINALLOCSIZE    (1 << MINBUCKET)
  215. #define BUCKETINDX(size) \
  216.     (size) <= (MINALLOCSIZE * 128) \
  217.         ? (size) <= (MINALLOCSIZE * 8) \
  218.             ? (size) <= (MINALLOCSIZE * 2) \
  219.                 ? (size) <= (MINALLOCSIZE * 1) \
  220.                     ? (MINBUCKET + 0) \
  221.                     : (MINBUCKET + 1) \
  222.                 : (size) <= (MINALLOCSIZE * 4) \
  223.                     ? (MINBUCKET + 2) \
  224.                     : (MINBUCKET + 3) \
  225.             : (size) <= (MINALLOCSIZE* 32) \
  226.                 ? (size) <= (MINALLOCSIZE * 16) \
  227.                     ? (MINBUCKET + 4) \
  228.                     : (MINBUCKET + 5) \
  229.                 : (size) <= (MINALLOCSIZE * 64) \
  230.                     ? (MINBUCKET + 6) \
  231.                     : (MINBUCKET + 7) \
  232.         : (size) <= (MINALLOCSIZE * 2048) \
  233.             ? (size) <= (MINALLOCSIZE * 512) \
  234.                 ? (size) <= (MINALLOCSIZE * 256) \
  235.                     ? (MINBUCKET + 8) \
  236.                     : (MINBUCKET + 9) \
  237.                 : (size) <= (MINALLOCSIZE * 1024) \
  238.                     ? (MINBUCKET + 10) \
  239.                     : (MINBUCKET + 11) \
  240.             : (size) <= (MINALLOCSIZE * 8192) \
  241.                 ? (size) <= (MINALLOCSIZE * 4096) \
  242.                     ? (MINBUCKET + 12) \
  243.                     : (MINBUCKET + 13) \
  244.                 : (size) <= (MINALLOCSIZE * 16384) \
  245.                     ? (MINBUCKET + 14) \
  246.                     : (MINBUCKET + 15)
  247.  
  248. /*
  249.  * Turn virtual addresses into kmem map indicies
  250.  */
  251. #define kmemxtob(alloc)    (kmembase + (alloc) * NBPG)
  252. #define btokmemx(addr)    (((caddr_t)(addr) - kmembase) / NBPG)
  253. #define btokup(addr)    (&kmemusage[((caddr_t)(addr) - kmembase) >> CLSHIFT])
  254.  
  255. /*
  256.  * Macro versions for the usual cases of malloc/free
  257.  */
  258. #define    MALLOC(space, cast, size, type, flags) \
  259.     (space) = (cast)bsd_malloc((u_long)(size), type, flags)
  260. #define FREE(addr, type) bsd_free((caddr_t)(addr), type)
  261.  
  262. extern struct kmemstats kmemstats[];
  263. extern struct kmemusage *kmemusage;
  264. extern char *kmembase;
  265. extern struct kmembuckets bucket[];
  266. extern void *bsd_malloc __P((unsigned long size, int type, int flags));
  267. extern void bsd_free __P((void *addr, int type));
  268. #endif /* KERNEL */
  269. #endif /* !_MALLOC_H_ */
  270.