TWCOS Kernel

Hex Artifact Content
Login

Artifact adb598b623fa85559e3cf501c04b583f8a7f84aa:


0000: 23 69 6e 63 6c 75 64 65 20 22 73 6c 61 62 2e 68  #include "slab.h
0010: 22 0a 0a 23 69 66 20 49 4e 54 45 52 46 41 43 45  "..#if INTERFACE
0020: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 74  .#include <sys/t
0030: 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  ypes.h>.#include
0040: 20 3c 73 74 64 64 65 66 2e 68 3e 0a 23 69 6e 63   <stddef.h>.#inc
0050: 6c 75 64 65 20 3c 73 74 64 69 6e 74 2e 68 3e 0a  lude <stdint.h>.
0060: 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
0070: 73 6c 61 62 5f 74 20 73 6c 61 62 5f 74 3b 0a 0a  slab_t slab_t;..
0080: 2f 2a 2a 0a 20 2a 20 53 6c 61 62 20 74 79 70 65  /**. * Slab type
0090: 20 64 65 73 63 72 69 70 74 6f 72 0a 20 2a 2f 0a   descriptor. */.
00a0: 73 74 72 75 63 74 20 73 6c 61 62 5f 74 79 70 65  struct slab_type
00b0: 5f 74 20 7b 0a 09 2f 2a 2a 20 53 6c 61 62 20 6d  _t {../** Slab m
00c0: 61 67 69 63 20 76 61 6c 75 65 20 2d 20 70 73 65  agic value - pse
00d0: 75 64 6f 2d 72 61 6e 64 6f 6d 20 2a 2f 0a 09 75  udo-random */..u
00e0: 69 6e 74 33 32 5f 74 20 6d 61 67 69 63 3b 0a 0a  int32_t magic;..
00f0: 09 2f 2a 2a 20 41 6c 6c 6f 63 61 74 69 6f 6e 20  ./** Allocation 
0100: 73 69 7a 65 20 2a 2f 0a 09 73 69 7a 65 5f 74 20  size */..size_t 
0110: 65 73 69 7a 65 3b 0a 0a 09 2f 2a 2a 20 53 69 7a  esize;.../** Siz
0120: 65 20 6f 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69  e of an allocati
0130: 6f 6e 20 73 6c 6f 74 20 2a 2f 0a 09 73 69 7a 65  on slot */..size
0140: 5f 74 20 73 6c 6f 74 73 69 7a 65 3b 0a 0a 09 2f  _t slotsize;.../
0150: 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f  ** Number of slo
0160: 74 73 20 70 65 72 20 73 6c 61 62 20 2a 2f 0a 09  ts per slab */..
0170: 69 6e 74 20 63 6f 75 6e 74 3b 0a 0a 23 69 66 64  int count;..#ifd
0180: 65 66 20 44 45 42 55 47 0a 09 2f 2a 2a 20 49 6e  ef DEBUG../** In
0190: 20 75 73 65 20 2a 2f 0a 09 69 6e 74 20 69 6e 75   use */..int inu
01a0: 73 65 3b 0a 23 65 6e 64 69 66 0a 0a 09 2f 2a 2a  se;.#endif.../**
01b0: 20 44 65 66 69 6e 69 74 69 6f 6e 20 73 6f 75 72   Definition sour
01c0: 63 65 20 2a 2f 0a 09 63 68 61 72 20 2a 20 73 6f  ce */..char * so
01d0: 75 72 63 65 3b 0a 09 2f 2a 2a 20 44 65 66 69 6e  urce;../** Defin
01e0: 69 74 69 6f 6e 20 73 6f 75 72 63 65 20 6c 69 6e  ition source lin
01f0: 65 20 2a 2f 0a 09 69 6e 74 20 6c 69 6e 65 3b 0a  e */..int line;.
0200: 0a 09 2f 2a 2a 20 46 69 72 73 74 20 73 6c 61 62  ../** First slab
0210: 20 77 69 74 68 20 66 72 65 65 20 73 6c 6f 74 20   with free slot 
0220: 2a 2f 0a 09 73 6c 61 62 5f 74 20 2a 20 66 69 72  */..slab_t * fir
0230: 73 74 3b 0a 0a 09 2f 2a 2a 20 53 6c 6f 74 20 6e  st;.../** Slot n
0240: 65 78 74 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 09  ext pointer */..
0250: 73 6c 61 62 5f 74 79 70 65 5f 74 20 2a 20 6e 65  slab_type_t * ne
0260: 78 74 3b 0a 09 2f 2a 2a 20 53 6c 6f 74 20 70 72  xt;../** Slot pr
0270: 65 76 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 09 73  ev pointer */..s
0280: 6c 61 62 5f 74 79 70 65 5f 74 20 2a 20 70 72 65  lab_type_t * pre
0290: 76 3b 0a 0a 09 2f 2a 2a 20 43 75 73 74 6f 6d 20  v;.../** Custom 
02a0: 6d 61 72 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  mark function */
02b0: 0a 09 76 6f 69 64 20 28 2a 6d 61 72 6b 29 28 76  ..void (*mark)(v
02c0: 6f 69 64 20 2a 29 3b 0a 0a 09 2f 2a 2a 20 43 75  oid *);.../** Cu
02d0: 73 74 6f 6d 20 66 69 6e 61 6c 69 7a 65 20 66 75  stom finalize fu
02e0: 6e 63 74 69 6f 6e 20 2a 2f 0a 09 76 6f 69 64 20  nction */..void 
02f0: 28 2a 66 69 6e 61 6c 69 7a 65 29 28 76 6f 69 64  (*finalize)(void
0300: 20 2a 29 3b 0a 0a 09 2f 2a 2a 20 4d 75 74 65 78   *);.../** Mutex
0310: 20 6c 6f 63 6b 20 2a 2f 0a 09 6d 75 74 65 78 5f   lock */..mutex_
0320: 74 20 6c 6f 63 6b 5b 31 5d 3b 0a 7d 3b 0a 0a 2f  t lock[1];.};../
0330: 2a 2a 0a 20 2a 20 57 65 61 6b 20 72 65 66 65 72  **. * Weak refer
0340: 65 6e 63 65 0a 20 2a 2f 0a 73 74 72 75 63 74 20  ence. */.struct 
0350: 73 6c 61 62 5f 77 65 61 6b 72 65 66 5f 74 20 7b  slab_weakref_t {
0360: 0a 09 2f 2a 2a 20 52 65 66 65 72 65 6e 63 65 20  ../** Reference 
0370: 2a 2f 0a 09 76 6f 69 64 20 2a 20 70 3b 0a 0a 09  */..void * p;...
0380: 2f 2a 2a 20 52 65 6d 61 69 6e 69 6e 67 20 47 43  /** Remaining GC
0390: 20 63 68 61 6e 63 65 73 20 62 65 66 6f 72 65 20   chances before 
03a0: 61 20 77 65 61 6b 20 72 65 66 65 72 65 6e 63 65  a weak reference
03b0: 20 69 73 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 09   is removed */..
03c0: 69 6e 74 20 63 68 61 6e 63 65 73 3b 0a 0a 09 2f  int chances;.../
03d0: 2a 2a 20 53 65 71 75 65 6e 63 65 20 6e 75 6d 62  ** Sequence numb
03e0: 65 72 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61  er of the alloca
03f0: 74 69 6f 6e 20 2a 2f 0a 09 75 69 6e 74 36 34 5f  tion */..uint64_
0400: 74 20 73 65 71 3b 0a 7d 3b 0a 0a 73 74 72 75 63  t seq;.};..struc
0410: 74 20 67 63 5f 73 74 61 74 73 5f 74 0a 7b 0a 09  t gc_stats_t.{..
0420: 73 69 7a 65 5f 74 20 61 6c 6c 6f 63 61 74 65 64  size_t allocated
0430: 3b 0a 09 73 69 7a 65 5f 74 20 69 6e 75 73 65 3b  ;..size_t inuse;
0440: 0a 09 73 69 7a 65 5f 74 20 70 65 61 6b 3b 0a 09  ..size_t peak;..
0450: 73 69 7a 65 5f 74 20 74 6f 74 61 6c 3b 0a 09 73  size_t total;..s
0460: 69 7a 65 5f 74 20 63 6c 65 61 6e 65 64 3b 0a 09  ize_t cleaned;..
0470: 69 6e 74 20 6d 61 78 63 6f 6e 74 65 78 74 3b 0a  int maxcontext;.
0480: 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 53 4c 41 42  };..#define SLAB
0490: 5f 54 59 50 45 28 73 2c 20 6d 2c 20 66 29 20 7b  _TYPE(s, m, f) {
04a0: 2e 6d 61 67 69 63 3d 30 2c 20 2e 65 73 69 7a 65  .magic=0, .esize
04b0: 3d 73 2c 20 2e 6d 61 72 6b 3d 6d 2c 20 2e 66 69  =s, .mark=m, .fi
04c0: 6e 61 6c 69 7a 65 3d 66 2c 20 2e 73 6f 75 72 63  nalize=f, .sourc
04d0: 65 3d 5f 5f 46 49 4c 45 5f 5f 2c 20 2e 6c 69 6e  e=__FILE__, .lin
04e0: 65 3d 5f 5f 4c 49 4e 45 5f 5f 7d 0a 0a 23 64 65  e=__LINE__}..#de
04f0: 66 69 6e 65 20 6d 63 6c 6f 6e 65 28 70 29 20 6d  fine mclone(p) m
0500: 63 6c 6f 6e 65 5f 73 69 7a 65 64 28 70 2c 20 73  clone_sized(p, s
0510: 69 7a 65 6f 66 28 2a 70 29 29 0a 0a 23 64 65 66  izeof(*p))..#def
0520: 69 6e 65 20 47 43 52 4f 4f 54 20 5f 5f 61 74 74  ine GCROOT __att
0530: 72 69 62 75 74 65 5f 5f 28 28 73 65 63 74 69 6f  ribute__((sectio
0540: 6e 28 22 2e 67 63 72 6f 6f 74 22 29 29 29 0a 0a  n(".gcroot")))..
0550: 23 65 6e 64 69 66 0a 0a 65 78 63 65 70 74 69 6f  #endif..exceptio
0560: 6e 5f 64 65 66 20 4f 75 74 4f 66 4d 65 6d 6f 72  n_def OutOfMemor
0570: 79 45 78 63 65 70 74 69 6f 6e 20 3d 20 7b 20 22  yException = { "
0580: 4f 75 74 4f 66 4d 65 6d 6f 72 79 45 78 63 65 70  OutOfMemoryExcep
0590: 74 69 6f 6e 22 2c 20 26 45 78 63 65 70 74 69 6f  tion", &Exceptio
05a0: 6e 20 7d 3b 0a 65 78 63 65 70 74 69 6f 6e 5f 64  n };.exception_d
05b0: 65 66 20 41 6c 6c 6f 63 61 74 69 6f 6e 54 6f 6f  ef AllocationToo
05c0: 42 69 67 45 78 63 65 70 74 69 6f 6e 20 3d 20 7b  BigException = {
05d0: 20 22 41 6c 6c 6f 63 61 74 69 6f 6e 54 6f 6f 42   "AllocationTooB
05e0: 69 67 45 78 63 65 70 74 69 6f 6e 22 2c 20 26 45  igException", &E
05f0: 78 63 65 70 74 69 6f 6e 20 7d 3b 0a 0a 74 79 70  xception };..typ
0600: 65 64 65 66 20 65 6e 75 6d 20 7b 20 67 63 66 72  edef enum { gcfr
0610: 65 65 2c 20 67 63 77 68 69 74 65 2c 20 67 63 67  ee, gcwhite, gcg
0620: 72 65 79 2c 20 67 63 62 6c 61 63 6b 20 7d 20 67  rey, gcblack } g
0630: 63 63 6f 6c 6f 75 72 3b 0a 74 79 70 65 64 65 66  ccolour;.typedef
0640: 20 73 74 72 75 63 74 20 73 6c 61 62 5f 73 6c 6f   struct slab_slo
0650: 74 5f 74 20 73 6c 61 62 5f 73 6c 6f 74 5f 74 3b  t_t slab_slot_t;
0660: 0a 0a 2f 2a 2a 0a 20 2a 20 53 6c 61 62 20 73 6c  ../**. * Slab sl
0670: 6f 74 20 2d 20 74 68 65 20 75 6e 69 74 20 6f 66  ot - the unit of
0680: 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 20 2a 2f 0a   allocation. */.
0690: 73 74 72 75 63 74 20 73 6c 61 62 5f 73 6c 6f 74  struct slab_slot
06a0: 5f 74 20 7b 0a 09 2f 2a 2a 20 73 65 71 75 65 6e  _t {../** sequen
06b0: 63 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ce number of the
06c0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2d 20 75 73   allocation - us
06d0: 65 64 20 62 79 20 74 68 65 20 77 65 6b 20 72 65  ed by the wek re
06e0: 66 65 72 65 6e 63 65 20 74 6f 20 64 65 74 65 63  ference to detec
06f0: 74 20 73 74 61 6c 65 20 72 65 66 65 72 65 6e 63  t stale referenc
0700: 65 73 20 2a 2f 0a 09 75 69 6e 74 36 34 5f 74 20  es */..uint64_t 
0710: 73 65 71 3b 0a 0a 09 2f 2a 2a 20 47 43 20 70 72  seq;.../** GC pr
0720: 65 76 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 09 73  ev pointer */..s
0730: 6c 61 62 5f 73 6c 6f 74 5f 74 20 2a 20 70 72 65  lab_slot_t * pre
0740: 76 3b 0a 09 2f 2a 2a 20 47 43 20 6e 65 78 74 20  v;../** GC next 
0750: 70 6f 69 6e 74 65 72 20 2a 2f 0a 09 73 6c 61 62  pointer */..slab
0760: 5f 73 6c 6f 74 5f 74 20 2a 20 6e 65 78 74 3b 0a  _slot_t * next;.
0770: 0a 09 2f 2a 2a 20 47 43 20 73 74 61 74 65 20 2d  ../** GC state -
0780: 20 69 6e 64 69 63 61 74 65 73 20 77 68 69 63 68   indicates which
0790: 20 47 43 20 6c 69 73 74 20 74 68 69 73 20 73 6c   GC list this sl
07a0: 6f 74 20 69 73 20 6f 6e 20 2a 2f 0a 09 67 63 63  ot is on */..gcc
07b0: 6f 6c 6f 75 72 20 63 6f 6c 6f 75 72 3b 0a 7d 3b  olour colour;.};
07c0: 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
07d0: 20 73 6c 61 62 5f 74 20 73 6c 61 62 5f 74 3b 0a   slab_t slab_t;.
07e0: 0a 2f 2a 2a 0a 20 2a 20 53 6c 61 62 20 2d 20 67  ./**. * Slab - g
07f0: 65 6e 65 72 61 6c 6c 79 20 70 61 67 65 64 20 73  enerally paged s
0800: 69 7a 65 0a 20 2a 2f 0a 73 74 72 75 63 74 20 73  ize. */.struct s
0810: 6c 61 62 5f 74 20 7b 0a 09 2f 2a 2a 20 4d 61 67  lab_t {../** Mag
0820: 69 63 20 6e 75 6d 62 65 72 20 74 6f 20 76 61 6c  ic number to val
0830: 69 64 61 74 65 20 61 67 61 69 6e 73 74 20 74 68  idate against th
0840: 65 20 74 79 70 65 20 69 6e 66 6f 72 6d 61 74 69  e type informati
0850: 6f 6e 20 2a 2f 0a 09 75 69 6e 74 33 32 5f 74 20  on */..uint32_t 
0860: 6d 61 67 69 63 3b 0a 09 2f 2a 2a 20 4e 65 78 74  magic;../** Next
0870: 20 73 6c 61 62 20 2a 2f 0a 09 73 6c 61 62 5f 74   slab */..slab_t
0880: 20 2a 20 6e 65 78 74 3b 0a 09 2f 2a 2a 20 50 72   * next;../** Pr
0890: 65 76 20 73 6c 61 62 20 2a 2f 0a 09 73 6c 61 62  ev slab */..slab
08a0: 5f 74 20 2a 20 70 72 65 76 3b 0a 0a 09 2f 2a 2a  _t * prev;.../**
08b0: 20 53 6c 61 62 20 74 79 70 65 20 69 6e 66 6f 72   Slab type infor
08c0: 6d 61 74 69 6f 6e 20 2a 2f 0a 09 73 6c 61 62 5f  mation */..slab_
08d0: 74 79 70 65 5f 74 20 2a 20 74 79 70 65 3b 0a 0a  type_t * type;..
08e0: 09 2f 2a 2a 20 46 69 72 73 74 20 66 72 65 65 20  ./** First free 
08f0: 73 6c 6f 74 20 6f 66 20 74 68 69 73 20 73 6c 61  slot of this sla
0900: 62 20 2a 2f 0a 09 73 6c 61 62 5f 73 6c 6f 74 5f  b */..slab_slot_
0910: 74 20 2a 20 66 72 65 65 3b 0a 7d 3b 0a 0a 67 63  t * free;.};..gc
0920: 5f 73 74 61 74 73 5f 74 20 67 63 5f 73 74 61 74  _stats_t gc_stat
0930: 73 20 3d 20 7b 30 7d 3b 0a 0a 73 74 61 74 69 63  s = {0};..static
0940: 20 73 6c 61 62 5f 74 79 70 65 5f 74 20 2a 20 74   slab_type_t * t
0950: 79 70 65 73 3b 0a 0a 23 69 66 20 30 0a 73 74 61  ypes;..#if 0.sta
0960: 74 69 63 20 74 68 72 65 61 64 5f 74 20 2a 20 63  tic thread_t * c
0970: 6c 65 61 6e 65 72 5f 74 68 72 65 61 64 3b 0a 69  leaner_thread;.i
0980: 6e 74 20 69 73 5f 63 6c 65 61 6e 65 72 28 29 0a  nt is_cleaner().
0990: 7b 0a 09 72 65 74 75 72 6e 20 61 72 63 68 5f 67  {..return arch_g
09a0: 65 74 5f 74 68 72 65 61 64 28 29 20 3d 3d 20 63  et_thread() == c
09b0: 6c 65 61 6e 65 72 5f 74 68 72 65 61 64 3b 0a 7d  leaner_thread;.}
09c0: 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c  ..static void cl
09d0: 65 61 6e 65 72 28 29 0a 7b 0a 09 63 6c 65 61 6e  eaner().{..clean
09e0: 65 72 5f 74 68 72 65 61 64 20 3d 20 61 72 63 68  er_thread = arch
09f0: 5f 67 65 74 5f 74 68 72 65 61 64 28 29 3b 0a 09  _get_thread();..
0a00: 69 6e 74 20 70 61 67 65 73 20 3d 20 70 61 67 65  int pages = page
0a10: 5f 63 6f 75 6e 74 28 29 3b 0a 09 77 68 69 6c 65  _count();..while
0a20: 28 31 29 20 7b 0a 09 09 4d 4f 4e 49 54 4f 52 5f  (1) {...MONITOR_
0a30: 41 55 54 4f 4c 4f 43 4b 28 63 6c 65 61 6e 73 69  AUTOLOCK(cleansi
0a40: 67 6e 61 6c 29 20 7b 0a 09 09 09 6d 6f 6e 69 74  gnal) {....monit
0a50: 6f 72 5f 77 61 69 74 28 63 6c 65 61 6e 73 69 67  or_wait(cleansig
0a60: 6e 61 6c 29 3b 0a 09 09 7d 0a 09 09 69 6e 74 20  nal);...}...int 
0a70: 70 72 65 66 72 65 65 20 3d 20 70 61 67 65 5f 63  prefree = page_c
0a80: 6f 75 6e 74 5f 66 72 65 65 28 29 3b 0a 09 09 74  ount_free();...t
0a90: 68 72 65 61 64 5f 67 63 28 29 3b 0a 09 09 69 6e  hread_gc();...in
0aa0: 74 20 70 6f 73 74 66 72 65 65 20 3d 20 70 61 67  t postfree = pag
0ab0: 65 5f 63 6f 75 6e 74 5f 66 72 65 65 28 29 3b 0a  e_count_free();.
0ac0: 09 09 4d 4f 4e 49 54 4f 52 5f 41 55 54 4f 4c 4f  ..MONITOR_AUTOLO
0ad0: 43 4b 28 66 72 65 65 73 69 67 6e 61 6c 29 20 7b  CK(freesignal) {
0ae0: 0a 09 09 09 6d 6f 6e 69 74 6f 72 5f 62 72 6f 61  ....monitor_broa
0af0: 64 63 61 73 74 28 66 72 65 65 73 69 67 6e 61 6c  dcast(freesignal
0b00: 29 3b 0a 09 09 7d 0a 09 7d 0a 7d 0a 23 65 6e 64  );...}..}.}.#end
0b10: 69 66 0a 0a 69 6e 74 20 73 6c 61 62 5f 69 64 6c  if..int slab_idl
0b20: 65 5f 67 63 28 29 0a 7b 0a 09 72 65 74 75 72 6e  e_gc().{..return
0b30: 20 67 63 5f 73 74 61 74 73 2e 61 6c 6c 6f 63 61   gc_stats.alloca
0b40: 74 65 64 20 3e 20 67 63 5f 73 74 61 74 73 2e 69  ted > gc_stats.i
0b50: 6e 75 73 65 3b 0a 7d 0a 0a 76 6f 69 64 20 73 6c  nuse;.}..void sl
0b60: 61 62 5f 69 6e 69 74 28 29 0a 7b 0a 7d 0a 0a 73  ab_init().{.}..s
0b70: 74 61 74 69 63 20 6d 75 74 65 78 5f 74 20 73 6c  tatic mutex_t sl
0b80: 61 62 6c 6f 63 6b 5b 31 5d 3b 0a 73 74 61 74 69  ablock[1];.stati
0b90: 63 20 76 6f 69 64 20 73 6c 61 62 5f 6c 6f 63 6b  c void slab_lock
0ba0: 28 69 6e 74 20 67 63 29 0a 7b 0a 09 69 66 20 28  (int gc).{..if (
0bb0: 67 63 29 20 7b 0a 09 09 6d 75 74 65 78 5f 6c 6f  gc) {...mutex_lo
0bc0: 63 6b 28 73 6c 61 62 6c 6f 63 6b 29 3b 0a 09 7d  ck(slablock);..}
0bd0: 20 65 6c 73 65 20 7b 0a 09 09 6d 75 74 65 78 5f   else {...mutex_
0be0: 6c 6f 63 6b 28 73 6c 61 62 6c 6f 63 6b 29 3b 0a  lock(slablock);.
0bf0: 09 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  .}.}..static voi
0c00: 64 20 73 6c 61 62 5f 75 6e 6c 6f 63 6b 28 29 0a  d slab_unlock().
0c10: 7b 0a 09 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28  {..mutex_unlock(
0c20: 73 6c 61 62 6c 6f 63 6b 29 3b 0a 7d 0a 0a 73 74  slablock);.}..st
0c30: 61 74 69 63 20 73 6c 61 62 5f 74 79 70 65 5f 74  atic slab_type_t
0c40: 20 2a 20 73 6c 61 62 5f 6d 69 6e 20 3d 20 30 3b   * slab_min = 0;
0c50: 0a 73 74 61 74 69 63 20 73 6c 61 62 5f 74 79 70  .static slab_typ
0c60: 65 5f 74 20 2a 20 73 6c 61 62 5f 6d 61 78 20 3d  e_t * slab_max =
0c70: 20 30 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20   0;.static void 
0c80: 73 6c 61 62 5f 72 65 67 69 73 74 65 72 28 73 6c  slab_register(sl
0c90: 61 62 5f 74 79 70 65 5f 74 20 2a 20 73 74 79 70  ab_type_t * styp
0ca0: 65 29 0a 7b 0a 09 69 66 20 28 30 20 3d 3d 20 73  e).{..if (0 == s
0cb0: 6c 61 62 5f 6d 69 6e 20 7c 7c 20 73 74 79 70 65  lab_min || stype
0cc0: 20 3c 20 73 6c 61 62 5f 6d 69 6e 29 20 7b 0a 09   < slab_min) {..
0cd0: 09 73 6c 61 62 5f 6d 69 6e 20 3d 20 73 74 79 70  .slab_min = styp
0ce0: 65 3b 0a 09 7d 0a 09 69 66 20 28 30 20 3d 3d 20  e;..}..if (0 == 
0cf0: 73 6c 61 62 5f 6d 61 78 20 7c 7c 20 73 74 79 70  slab_max || styp
0d00: 65 20 3e 20 73 6c 61 62 5f 6d 61 78 29 20 7b 0a  e > slab_max) {.
0d10: 09 09 73 6c 61 62 5f 6d 61 78 20 3d 20 73 74 79  ..slab_max = sty
0d20: 70 65 3b 0a 09 7d 0a 7d 0a 0a 73 74 61 74 69 63  pe;..}.}..static
0d30: 20 69 6e 74 20 73 6c 61 62 5f 76 61 6c 69 64 28   int slab_valid(
0d40: 73 6c 61 62 5f 74 79 70 65 5f 74 20 2a 20 73 74  slab_type_t * st
0d50: 79 70 65 29 0a 7b 0a 09 72 65 74 75 72 6e 20 28  ype).{..return (
0d60: 73 6c 61 62 5f 6d 69 6e 20 3c 3d 20 73 74 79 70  slab_min <= styp
0d70: 65 20 26 26 20 73 74 79 70 65 20 3c 3d 20 73 6c  e && stype <= sl
0d80: 61 62 5f 6d 61 78 29 3b 0a 7d 0a 0a 73 74 61 74  ab_max);.}..stat
0d90: 69 63 20 76 6f 69 64 20 64 65 62 75 67 5f 66 69  ic void debug_fi
0da0: 6c 6c 62 75 66 28 76 6f 69 64 20 2a 20 70 2c 20  llbuf(void * p, 
0db0: 73 69 7a 65 5f 74 20 6c 2c 20 69 6e 74 20 63 29  size_t l, int c)
0dc0: 0a 7b 0a 23 69 66 64 65 66 20 44 45 42 55 47 0a  .{.#ifdef DEBUG.
0dd0: 09 6d 65 6d 73 65 74 28 70 2c 20 63 2c 20 6c 29  .memset(p, c, l)
0de0: 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 73 74 61 74  ;.#endif.}..stat
0df0: 69 63 20 69 6e 74 65 72 72 75 70 74 5f 6d 6f 6e  ic interrupt_mon
0e00: 69 74 6f 72 5f 74 20 6c 69 73 74 5f 6d 6f 76 65  itor_t list_move
0e10: 5f 6c 6f 63 6b 5b 31 5d 3b 0a 0a 73 74 61 74 69  _lock[1];..stati
0e20: 63 20 76 6f 69 64 20 73 6c 61 62 5f 73 6c 6f 74  c void slab_slot
0e30: 5f 66 72 65 65 28 73 6c 61 62 5f 73 6c 6f 74 5f  _free(slab_slot_
0e40: 74 20 2a 20 73 6c 6f 74 29 0a 7b 0a 09 73 6c 61  t * slot).{..sla
0e50: 62 5f 74 20 2a 20 73 6c 61 62 20 3d 20 41 52 43  b_t * slab = ARC
0e60: 48 5f 50 41 47 45 5f 41 4c 49 47 4e 28 73 6c 6f  H_PAGE_ALIGN(slo
0e70: 74 29 3b 0a 09 69 66 20 28 30 20 3d 3d 20 73 6c  t);..if (0 == sl
0e80: 61 62 2d 3e 66 72 65 65 29 20 7b 0a 09 09 2f 2a  ab->free) {.../*
0e90: 20 50 72 65 76 69 6f 75 73 6c 79 20 65 6d 70 74   Previously empt
0ea0: 79 2c 20 6e 6f 77 20 68 61 73 20 61 76 61 69 6c  y, now has avail
0eb0: 61 62 6c 65 20 73 6c 6f 74 73 20 2a 2f 0a 09 09  able slots */...
0ec0: 4c 49 53 54 5f 41 50 50 45 4e 44 28 73 6c 61 62  LIST_APPEND(slab
0ed0: 2d 3e 74 79 70 65 2d 3e 66 69 72 73 74 2c 20 73  ->type->first, s
0ee0: 6c 61 62 29 3b 0a 09 7d 0a 09 73 6c 6f 74 2d 3e  lab);..}..slot->
0ef0: 73 65 71 20 3d 20 30 3b 09 0a 09 73 6c 6f 74 2d  seq = 0;...slot-
0f00: 3e 63 6f 6c 6f 75 72 20 3d 20 67 63 66 72 65 65  >colour = gcfree
0f10: 3b 0a 09 64 65 62 75 67 5f 66 69 6c 6c 62 75 66  ;..debug_fillbuf
0f20: 28 73 6c 6f 74 2b 31 2c 20 73 6c 61 62 2d 3e 74  (slot+1, slab->t
0f30: 79 70 65 2d 3e 65 73 69 7a 65 2c 20 30 78 66 38  ype->esize, 0xf8
0f40: 29 3b 0a 09 4c 49 53 54 5f 41 50 50 45 4e 44 28  );..LIST_APPEND(
0f50: 73 6c 61 62 2d 3e 66 72 65 65 2c 20 73 6c 6f 74  slab->free, slot
0f60: 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 73 6c 61  );.}..static sla
0f70: 62 5f 74 20 2a 20 73 6c 61 62 5f 6e 65 77 28 73  b_t * slab_new(s
0f80: 6c 61 62 5f 74 79 70 65 5f 74 20 2a 20 73 74 79  lab_type_t * sty
0f90: 70 65 29 0a 7b 0a 09 69 66 20 28 30 20 3d 3d 20  pe).{..if (0 == 
0fa0: 73 74 79 70 65 2d 3e 6d 61 67 69 63 29 20 7b 0a  stype->magic) {.
0fb0: 09 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  ../* Initialize 
0fc0: 74 79 70 65 20 2a 2f 0a 09 09 73 74 79 70 65 2d  type */...stype-
0fd0: 3e 66 69 72 73 74 20 3d 20 30 3b 0a 09 09 73 74  >first = 0;...st
0fe0: 79 70 65 2d 3e 6d 61 67 69 63 20 3d 20 39 39 37  ype->magic = 997
0ff0: 20 2a 20 28 75 69 6e 74 33 32 5f 74 29 73 74 79   * (uint32_t)sty
1000: 70 65 3b 0a 09 09 73 74 79 70 65 2d 3e 73 6c 6f  pe;...stype->slo
1010: 74 73 69 7a 65 20 3d 20 52 4f 55 4e 44 55 50 28  tsize = ROUNDUP(
1020: 73 74 79 70 65 2d 3e 65 73 69 7a 65 2b 73 69 7a  stype->esize+siz
1030: 65 6f 66 28 73 6c 61 62 5f 73 6c 6f 74 5f 74 29  eof(slab_slot_t)
1040: 2c 20 73 69 7a 65 6f 66 28 69 6e 74 70 74 72 5f  , sizeof(intptr_
1050: 74 29 29 3b 0a 09 09 73 74 79 70 65 2d 3e 63 6f  t));...stype->co
1060: 75 6e 74 20 3d 20 28 41 52 43 48 5f 50 41 47 45  unt = (ARCH_PAGE
1070: 5f 53 49 5a 45 2d 73 69 7a 65 6f 66 28 73 6c 61  _SIZE-sizeof(sla
1080: 62 5f 74 29 29 20 2f 20 73 74 79 70 65 2d 3e 73  b_t)) / stype->s
1090: 6c 6f 74 73 69 7a 65 3b 0a 09 09 49 4e 54 45 52  lotsize;...INTER
10a0: 52 55 50 54 5f 4d 4f 4e 49 54 4f 52 5f 41 55 54  RUPT_MONITOR_AUT
10b0: 4f 4c 4f 43 4b 28 6c 69 73 74 5f 6d 6f 76 65 5f  OLOCK(list_move_
10c0: 6c 6f 63 6b 29 20 7b 0a 09 09 09 4c 49 53 54 5f  lock) {....LIST_
10d0: 41 50 50 45 4e 44 28 74 79 70 65 73 2c 20 73 74  APPEND(types, st
10e0: 79 70 65 29 3b 0a 09 09 7d 0a 09 09 73 6c 61 62  ype);...}...slab
10f0: 5f 72 65 67 69 73 74 65 72 28 73 74 79 70 65 29  _register(stype)
1100: 3b 0a 09 7d 0a 0a 09 2f 2a 20 41 6c 6c 6f 63 61  ;..}.../* Alloca
1110: 74 65 20 61 6e 64 20 6d 61 70 20 70 61 67 65 20  te and map page 
1120: 2a 2f 0a 09 73 6c 61 62 5f 74 20 2a 20 73 6c 61  */..slab_t * sla
1130: 62 20 3d 20 70 61 67 65 5f 68 65 61 70 5f 61 6c  b = page_heap_al
1140: 6c 6f 63 28 29 3b 0a 09 69 66 20 28 73 6c 61 62  loc();..if (slab
1150: 29 20 7b 0a 09 09 6d 65 6d 73 65 74 28 73 6c 61  ) {...memset(sla
1160: 62 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 73 6c  b, 0, sizeof(*sl
1170: 61 62 29 29 3b 0a 09 09 73 6c 61 62 2d 3e 6d 61  ab));...slab->ma
1180: 67 69 63 20 3d 20 73 74 79 70 65 2d 3e 6d 61 67  gic = stype->mag
1190: 69 63 3b 0a 09 09 73 6c 61 62 2d 3e 74 79 70 65  ic;...slab->type
11a0: 20 3d 20 73 74 79 70 65 3b 0a 0a 09 09 2f 2a 20   = stype;..../* 
11b0: 50 75 74 20 65 61 63 68 20 73 6c 6f 74 20 6f 6e  Put each slot on
11c0: 74 6f 20 74 68 65 20 66 72 65 65 20 6c 69 73 74  to the free list
11d0: 20 2a 2f 0a 09 09 73 6c 61 62 5f 73 6c 6f 74 5f   */...slab_slot_
11e0: 74 20 2a 20 73 6c 6f 74 20 3d 20 28 73 6c 61 62  t * slot = (slab
11f0: 5f 73 6c 6f 74 5f 74 2a 29 28 73 6c 61 62 2b 31  _slot_t*)(slab+1
1200: 29 3b 0a 09 09 49 4e 54 45 52 52 55 50 54 5f 4d  );...INTERRUPT_M
1210: 4f 4e 49 54 4f 52 5f 41 55 54 4f 4c 4f 43 4b 28  ONITOR_AUTOLOCK(
1220: 6c 69 73 74 5f 6d 6f 76 65 5f 6c 6f 63 6b 29 20  list_move_lock) 
1230: 7b 0a 09 09 09 66 6f 72 28 69 6e 74 20 69 3d 30  {....for(int i=0
1240: 3b 20 69 3c 73 74 79 70 65 2d 3e 63 6f 75 6e 74  ; i<stype->count
1250: 3b 20 69 2b 2b 2c 20 73 6c 6f 74 20 3d 20 50 54  ; i++, slot = PT
1260: 52 5f 42 59 54 45 5f 41 44 44 52 45 53 53 28 73  R_BYTE_ADDRESS(s
1270: 6c 6f 74 2c 20 73 74 79 70 65 2d 3e 73 6c 6f 74  lot, stype->slot
1280: 73 69 7a 65 29 29 20 7b 0a 09 09 09 09 73 6c 61  size)) {.....sla
1290: 62 5f 73 6c 6f 74 5f 66 72 65 65 28 73 6c 6f 74  b_slot_free(slot
12a0: 29 3b 0a 09 09 09 7d 0a 09 09 7d 0a 09 7d 0a 0a  );....}...}..}..
12b0: 09 72 65 74 75 72 6e 20 73 6c 61 62 3b 0a 7d 0a  .return slab;.}.
12c0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62  .static void deb
12d0: 75 67 5f 63 68 65 63 6b 62 75 66 28 76 6f 69 64  ug_checkbuf(void
12e0: 20 2a 20 70 2c 20 73 69 7a 65 5f 74 20 6c 29 0a   * p, size_t l).
12f0: 7b 0a 23 69 66 64 65 66 20 44 45 42 55 47 0a 09  {.#ifdef DEBUG..
1300: 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 70 20  const char * cp 
1310: 3d 20 70 3b 0a 09 63 68 61 72 20 63 20 3d 20 2a  = p;..char c = *
1320: 63 70 3b 0a 0a 09 66 6f 72 28 69 6e 74 20 69 3d  cp;...for(int i=
1330: 30 3b 20 69 3c 6c 3b 20 69 2b 2b 29 0a 09 7b 0a  0; i<l; i++)..{.
1340: 09 09 69 66 20 28 63 20 21 3d 20 2a 63 70 29 20  ..if (c != *cp) 
1350: 7b 0a 09 09 09 6b 65 72 6e 65 6c 5f 70 61 6e 69  {....kernel_pani
1360: 63 28 22 43 6f 72 72 75 70 74 65 64 20 62 75 66  c("Corrupted buf
1370: 66 65 72 3a 20 25 70 22 2c 20 70 29 3b 0a 09 09  fer: %p", p);...
1380: 7d 0a 09 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a  }..}.#endif.}...
1390: 23 64 65 66 69 6e 65 20 53 4c 41 42 5f 53 4c 4f  #define SLAB_SLO
13a0: 54 28 73 6c 61 62 2c 20 73 6c 6f 74 29 20 28 28  T(slab, slot) ((
13b0: 73 6c 61 62 5f 73 6c 6f 74 5f 74 2a 29 28 50 54  slab_slot_t*)(PT
13c0: 52 5f 42 59 54 45 5f 41 44 44 52 45 53 53 28 73  R_BYTE_ADDRESS(s
13d0: 6c 61 62 2b 31 2c 20 73 6c 61 62 2d 3e 74 79 70  lab+1, slab->typ
13e0: 65 2d 3e 73 6c 6f 74 73 69 7a 65 2a 73 6c 6f 74  e->slotsize*slot
13f0: 29 29 29 0a 23 64 65 66 69 6e 65 20 53 4c 41 42  ))).#define SLAB
1400: 5f 53 4c 4f 54 5f 55 53 45 52 28 73 6c 61 62 2c  _SLOT_USER(slab,
1410: 20 73 6c 6f 74 29 20 28 53 4c 41 42 5f 53 4c 4f   slot) (SLAB_SLO
1420: 54 28 73 6c 61 62 2c 20 73 6c 6f 74 29 2b 31 29  T(slab, slot)+1)
1430: 0a 0a 23 64 65 66 69 6e 65 20 53 4c 41 42 5f 53  ..#define SLAB_S
1440: 4c 4f 54 5f 4e 55 4d 28 73 6c 61 62 2c 20 70 29  LOT_NUM(slab, p)
1450: 20 28 50 54 52 5f 42 59 54 45 5f 44 49 46 46 28   (PTR_BYTE_DIFF(
1460: 73 6c 61 62 2b 31 2c 20 70 29 20 2f 20 73 6c 61  slab+1, p) / sla
1470: 62 2d 3e 74 79 70 65 2d 3e 73 6c 6f 74 73 69 7a  b->type->slotsiz
1480: 65 29 0a 0a 2f 2a 2a 20 41 6c 6c 6f 63 61 74 65  e)../** Allocate
1490: 64 20 62 75 74 20 6e 6f 74 20 6d 61 72 6b 65 64  d but not marked
14a0: 20 79 65 74 20 2a 2f 0a 73 74 61 74 69 63 20 73   yet */.static s
14b0: 6c 61 62 5f 73 6c 6f 74 5f 74 20 2a 20 77 68 69  lab_slot_t * whi
14c0: 74 65 3b 0a 0a 2f 2a 2a 20 41 6c 6c 6f 63 61 74  te;../** Allocat
14d0: 65 64 2c 20 6d 61 72 6b 65 64 2c 20 62 75 74 20  ed, marked, but 
14e0: 6e 6f 74 20 73 63 61 6e 6e 65 64 20 2a 2f 0a 73  not scanned */.s
14f0: 74 61 74 69 63 20 73 6c 61 62 5f 73 6c 6f 74 5f  tatic slab_slot_
1500: 74 20 2a 20 67 72 65 79 3b 0a 0a 2f 2a 2a 20 41  t * grey;../** A
1510: 6c 6c 6f 63 61 74 65 64 2c 20 6d 61 72 6b 65 64  llocated, marked
1520: 2c 20 61 6e 64 20 73 63 61 6e 6e 65 64 20 2a 2f  , and scanned */
1530: 0a 73 74 61 74 69 63 20 73 6c 61 62 5f 73 6c 6f  .static slab_slo
1540: 74 5f 74 20 2a 20 62 6c 61 63 6b 3b 0a 0a 2f 2a  t_t * black;../*
1550: 2a 20 47 61 72 62 61 67 65 2c 20 62 75 74 20 6e  * Garbage, but n
1560: 6f 74 20 79 65 74 20 73 77 65 70 74 20 2a 2f 0a  ot yet swept */.
1570: 73 74 61 74 69 63 20 73 6c 61 62 5f 73 6c 6f 74  static slab_slot
1580: 5f 74 20 2a 20 67 61 72 62 61 67 65 3b 0a 0a 73  _t * garbage;..s
1590: 74 61 74 69 63 20 69 6e 74 20 73 6c 61 62 5f 67  tatic int slab_g
15a0: 63 5f 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 73 77  c_incremental_sw
15b0: 65 65 70 5f 6c 6f 63 6b 65 64 28 73 6c 61 62 5f  eep_locked(slab_
15c0: 74 79 70 65 5f 74 20 2a 20 75 6e 74 69 6c 29 3b  type_t * until);
15d0: 0a 76 6f 69 64 20 2a 20 73 6c 61 62 5f 61 6c 6c  .void * slab_all
15e0: 6f 63 28 73 6c 61 62 5f 74 79 70 65 5f 74 20 2a  oc(slab_type_t *
15f0: 20 73 74 79 70 65 29 0a 7b 0a 09 76 6f 69 64 20   stype).{..void 
1600: 2a 20 70 20 3d 20 30 3b 0a 09 73 6c 61 62 5f 6c  * p = 0;..slab_l
1610: 6f 63 6b 28 30 29 3b 0a 09 4d 55 54 45 58 5f 41  ock(0);..MUTEX_A
1620: 55 54 4f 4c 4f 43 4b 28 73 74 79 70 65 2d 3e 6c  UTOLOCK(stype->l
1630: 6f 63 6b 29 0a 09 7b 0a 09 09 69 6e 74 20 73 77  ock)..{...int sw
1640: 65 70 74 20 3d 20 30 3b 0a 09 09 77 68 69 6c 65  ept = 0;...while
1650: 28 21 73 74 79 70 65 2d 3e 66 69 72 73 74 29 20  (!stype->first) 
1660: 7b 0a 09 09 09 69 66 20 28 73 77 65 70 74 29 20  {....if (swept) 
1670: 7b 0a 09 09 09 09 73 74 79 70 65 2d 3e 66 69 72  {.....stype->fir
1680: 73 74 20 3d 20 73 6c 61 62 5f 6e 65 77 28 73 74  st = slab_new(st
1690: 79 70 65 29 3b 0a 09 09 09 09 69 66 20 28 21 73  ype);.....if (!s
16a0: 74 79 70 65 2d 3e 66 69 72 73 74 29 20 7b 0a 09  type->first) {..
16b0: 09 09 09 09 2f 2a 20 4e 65 65 64 20 74 6f 20 47  ..../* Need to G
16c0: 43 20 2a 2f 0a 09 09 09 09 7d 0a 09 09 09 7d 20  C */.....}....} 
16d0: 65 6c 73 65 20 7b 0a 09 09 09 09 73 6c 61 62 5f  else {.....slab_
16e0: 67 63 5f 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 73  gc_incremental_s
16f0: 77 65 65 70 5f 6c 6f 63 6b 65 64 28 73 74 79 70  weep_locked(styp
1700: 65 29 3b 0a 09 09 09 09 73 77 65 70 74 20 3d 20  e);.....swept = 
1710: 31 3b 0a 09 09 09 7d 0a 09 09 7d 0a 09 09 73 6c  1;....}...}...sl
1720: 61 62 5f 74 20 2a 20 73 6c 61 62 20 3d 20 73 74  ab_t * slab = st
1730: 79 70 65 2d 3e 66 69 72 73 74 3b 0a 0a 09 09 61  ype->first;....a
1740: 73 73 65 72 74 28 73 6c 61 62 2d 3e 66 72 65 65  ssert(slab->free
1750: 29 3b 0a 09 09 73 6c 61 62 5f 73 6c 6f 74 5f 74  );...slab_slot_t
1760: 20 2a 20 73 6c 6f 74 20 3d 20 73 6c 61 62 2d 3e   * slot = slab->
1770: 66 72 65 65 3b 0a 09 09 61 73 73 65 72 74 28 73  free;...assert(s
1780: 6c 6f 74 29 3b 0a 09 09 61 73 73 65 72 74 28 73  lot);...assert(s
1790: 6c 6f 74 2d 3e 63 6f 6c 6f 75 72 20 3d 3d 20 67  lot->colour == g
17a0: 63 66 72 65 65 29 3b 0a 09 09 2f 2a 20 54 4f 44  cfree);.../* TOD
17b0: 4f 3a 20 43 6f 6e 63 75 72 72 65 6e 74 20 47 43  O: Concurrent GC
17c0: 20 77 6f 75 6c 64 20 70 75 74 20 74 68 69 73 20   would put this 
17d0: 6f 6e 20 74 68 65 20 67 72 65 79 20 6c 69 73 74  on the grey list
17e0: 20 2a 2f 0a 09 09 69 66 20 28 31 29 20 7b 0a 09   */...if (1) {..
17f0: 09 09 49 4e 54 45 52 52 55 50 54 5f 4d 4f 4e 49  ..INTERRUPT_MONI
1800: 54 4f 52 5f 41 55 54 4f 4c 4f 43 4b 28 6c 69 73  TOR_AUTOLOCK(lis
1810: 74 5f 6d 6f 76 65 5f 6c 6f 63 6b 29 20 7b 0a 09  t_move_lock) {..
1820: 09 09 09 4c 49 53 54 5f 44 45 4c 45 54 45 28 73  ...LIST_DELETE(s
1830: 6c 61 62 2d 3e 66 72 65 65 2c 20 73 6c 6f 74 29  lab->free, slot)
1840: 3b 0a 09 09 09 09 73 6c 6f 74 2d 3e 63 6f 6c 6f  ;.....slot->colo
1850: 75 72 20 3d 20 67 63 77 68 69 74 65 3b 0a 09 09  ur = gcwhite;...
1860: 09 09 4c 49 53 54 5f 41 50 50 45 4e 44 28 77 68  ..LIST_APPEND(wh
1870: 69 74 65 2c 20 73 6c 6f 74 29 3b 0a 0a 09 09 09  ite, slot);.....
1880: 09 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68 69  ./* Check if thi
1890: 73 20 77 61 73 20 74 68 65 20 6c 61 73 74 20 66  s was the last f
18a0: 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 09 09 09 09  ree slot */.....
18b0: 69 66 20 28 21 73 6c 61 62 2d 3e 66 72 65 65 29  if (!slab->free)
18c0: 20 7b 0a 09 09 09 09 09 4c 49 53 54 5f 44 45 4c   {......LIST_DEL
18d0: 45 54 45 28 73 74 79 70 65 2d 3e 66 69 72 73 74  ETE(stype->first
18e0: 2c 20 73 6c 61 62 29 3b 0a 09 09 09 09 7d 0a 09  , slab);.....}..
18f0: 09 09 7d 0a 09 09 7d 0a 09 09 67 63 5f 73 74 61  ..}...}...gc_sta
1900: 74 73 2e 61 6c 6c 6f 63 61 74 65 64 20 2b 3d 20  ts.allocated += 
1910: 73 6c 61 62 2d 3e 74 79 70 65 2d 3e 65 73 69 7a  slab->type->esiz
1920: 65 3b 0a 0a 09 09 2f 2a 20 53 65 71 75 65 6e 63  e;..../* Sequenc
1930: 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 77 65 61  e number for wea
1940: 6b 20 72 65 66 65 72 65 6e 63 65 20 73 75 70 70  k reference supp
1950: 6f 72 74 20 2a 2f 0a 09 09 73 74 61 74 69 63 20  ort */...static 
1960: 75 69 6e 74 36 34 5f 74 20 73 65 71 20 3d 20 30  uint64_t seq = 0
1970: 3b 0a 09 09 73 6c 6f 74 2d 3e 73 65 71 20 3d 20  ;...slot->seq = 
1980: 2b 2b 73 65 71 3b 0a 0a 09 09 2f 2a 20 70 20 69  ++seq;..../* p i
1990: 73 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 62  s the returned b
19a0: 75 66 66 65 72 20 70 6f 69 6e 74 65 72 20 2a 2f  uffer pointer */
19b0: 0a 09 09 70 20 3d 20 73 6c 6f 74 2b 31 3b 0a 09  ...p = slot+1;..
19c0: 09 64 65 62 75 67 5f 63 68 65 63 6b 62 75 66 28  .debug_checkbuf(
19d0: 70 2c 20 73 6c 61 62 2d 3e 74 79 70 65 2d 3e 65  p, slab->type->e
19e0: 73 69 7a 65 29 3b 0a 09 09 64 65 62 75 67 5f 66  size);...debug_f
19f0: 69 6c 6c 62 75 66 28 70 2c 20 73 6c 61 62 2d 3e  illbuf(p, slab->
1a00: 74 79 70 65 2d 3e 65 73 69 7a 65 2c 20 30 78 30  type->esize, 0x0
1a10: 61 29 3b 0a 23 69 66 64 65 66 20 44 45 42 55 47  a);.#ifdef DEBUG
1a20: 0a 09 09 73 74 79 70 65 2d 3e 69 6e 75 73 65 2b  ...stype->inuse+
1a30: 2b 3b 0a 23 65 6e 64 69 66 0a 09 7d 0a 09 73 6c  +;.#endif..}..sl
1a40: 61 62 5f 75 6e 6c 6f 63 6b 28 29 3b 0a 09 72 65  ab_unlock();..re
1a50: 74 75 72 6e 20 70 3b 0a 7d 0a 0a 76 6f 69 64 20  turn p;.}..void 
1a60: 2a 20 73 6c 61 62 5f 63 61 6c 6c 6f 63 28 73 6c  * slab_calloc(sl
1a70: 61 62 5f 74 79 70 65 5f 74 20 2a 20 73 74 79 70  ab_type_t * styp
1a80: 65 29 0a 7b 0a 09 76 6f 69 64 20 2a 20 70 20 3d  e).{..void * p =
1a90: 20 73 6c 61 62 5f 61 6c 6c 6f 63 28 73 74 79 70   slab_alloc(styp
1aa0: 65 29 3b 0a 09 6d 65 6d 73 65 74 28 70 2c 20 30  e);..memset(p, 0
1ab0: 2c 20 73 74 79 70 65 2d 3e 65 73 69 7a 65 29 3b  , stype->esize);
1ac0: 0a 0a 09 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  ...return p;.}..
1ad0: 73 74 61 74 69 63 20 69 6e 74 20 73 6c 61 62 5f  static int slab_
1ae0: 61 6c 6c 5f 66 72 65 65 28 73 6c 61 62 5f 74 20  all_free(slab_t 
1af0: 2a 20 73 6c 61 62 29 0a 7b 0a 09 69 6e 74 20 66  * slab).{..int f
1b00: 72 65 65 63 6f 75 6e 74 20 3d 20 30 3b 0a 09 73  reecount = 0;..s
1b10: 6c 61 62 5f 73 6c 6f 74 5f 74 20 2a 20 66 72 65  lab_slot_t * fre
1b20: 65 20 3d 20 73 6c 61 62 2d 3e 66 72 65 65 3b 0a  e = slab->free;.
1b30: 0a 09 77 68 69 6c 65 28 66 72 65 65 29 20 7b 0a  ..while(free) {.
1b40: 09 09 61 73 73 65 72 74 28 66 72 65 65 2d 3e 63  ..assert(free->c
1b50: 6f 6c 6f 75 72 20 3d 3d 20 67 63 66 72 65 65 29  olour == gcfree)
1b60: 3b 0a 09 09 66 72 65 65 63 6f 75 6e 74 2b 2b 3b  ;...freecount++;
1b70: 0a 09 09 4c 49 53 54 5f 4e 45 58 54 28 73 6c 61  ...LIST_NEXT(sla
1b80: 62 2d 3e 66 72 65 65 2c 20 66 72 65 65 29 3b 0a  b->free, free);.
1b90: 09 7d 0a 0a 09 61 73 73 65 72 74 28 66 72 65 65  .}...assert(free
1ba0: 63 6f 75 6e 74 3c 3d 73 6c 61 62 2d 3e 74 79 70  count<=slab->typ
1bb0: 65 2d 3e 63 6f 75 6e 74 29 3b 0a 0a 09 72 65 74  e->count);...ret
1bc0: 75 72 6e 20 66 72 65 65 63 6f 75 6e 74 20 3d 3d  urn freecount ==
1bd0: 20 73 6c 61 62 2d 3e 74 79 70 65 2d 3e 63 6f 75   slab->type->cou
1be0: 6e 74 3b 0a 7d 0a 0a 76 6f 69 64 20 73 6c 61 62  nt;.}..void slab
1bf0: 5f 6e 6f 6d 61 72 6b 28 76 6f 69 64 20 2a 20 70  _nomark(void * p
1c00: 29 0a 7b 0a 09 2f 2a 20 44 6f 65 73 20 6e 6f 74  ).{../* Does not
1c10: 68 69 6e 67 20 2a 2f 0a 7d 0a 0a 73 74 61 74 69  hing */.}..stati
1c20: 63 20 73 74 72 75 63 74 20 67 63 63 6f 6e 74 65  c struct gcconte
1c30: 78 74 20 7b 0a 09 2f 2a 20 54 68 65 20 62 6c 6f  xt {../* The blo
1c40: 63 6b 20 74 6f 20 62 65 20 73 63 61 6e 6e 65 64  ck to be scanned
1c50: 20 2a 2f 0a 09 76 6f 69 64 20 2a 2a 20 66 72 6f   */..void ** fro
1c60: 6d 3b 0a 09 76 6f 69 64 20 2a 2a 20 74 6f 3b 0a  m;..void ** to;.
1c70: 7d 20 63 6f 6e 74 65 78 74 5b 31 36 5d 3b 0a 0a  } context[16];..
1c80: 73 74 61 74 69 63 20 69 6e 74 20 67 63 6c 65 76  static int gclev
1c90: 65 6c 20 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20  el = 0;..#ifdef 
1ca0: 44 45 42 55 47 0a 73 74 61 74 69 63 20 76 6f 69  DEBUG.static voi
1cb0: 64 20 73 6c 61 62 5f 67 63 5f 64 75 6d 70 5f 74  d slab_gc_dump_t
1cc0: 79 70 65 73 28 29 0a 7b 0a 23 69 66 20 30 0a 09  ypes().{.#if 0..
1cd0: 73 6c 61 62 5f 74 79 70 65 5f 74 20 2a 20 73 74  slab_type_t * st
1ce0: 79 70 65 20 3d 20 74 79 70 65 73 3b 0a 09 77 68  ype = types;..wh
1cf0: 69 6c 65 28 73 74 79 70 65 29 20 7b 0a 09 09 6b  ile(stype) {...k
1d00: 65 72 6e 65 6c 5f 64 65 62 75 67 28 22 73 74 79  ernel_debug("sty
1d10: 70 65 20 25 73 3a 25 64 3a 20 65 73 69 7a 65 20  pe %s:%d: esize 
1d20: 25 64 20 2d 20 69 6e 75 73 65 20 25 64 5c 6e 22  %d - inuse %d\n"
1d30: 2c 20 73 74 79 70 65 2d 3e 73 6f 75 72 63 65 2c  , stype->source,
1d40: 20 73 74 79 70 65 2d 3e 6c 69 6e 65 2c 20 73 74   stype->line, st
1d50: 79 70 65 2d 3e 65 73 69 7a 65 2c 20 73 74 79 70  ype->esize, styp
1d60: 65 2d 3e 69 6e 75 73 65 29 3b 0a 09 09 4c 49 53  e->inuse);...LIS
1d70: 54 5f 4e 45 58 54 28 74 79 70 65 73 2c 20 73 74  T_NEXT(types, st
1d80: 79 70 65 29 3b 0a 09 7d 0a 23 65 6e 64 69 66 0a  ype);..}.#endif.
1d90: 7d 0a 23 65 6e 64 69 66 0a 0a 76 6f 69 64 20 73  }.#endif..void s
1da0: 6c 61 62 5f 67 63 5f 62 65 67 69 6e 28 29 0a 7b  lab_gc_begin().{
1db0: 0a 09 73 6c 61 62 5f 6c 6f 63 6b 28 31 29 3b 0a  ..slab_lock(1);.
1dc0: 0a 09 67 63 5f 73 74 61 74 73 2e 61 6c 6c 6f 63  ..gc_stats.alloc
1dd0: 61 74 65 64 20 3d 20 30 3b 0a 09 67 63 5f 73 74  ated = 0;..gc_st
1de0: 61 74 73 2e 69 6e 75 73 65 20 3d 20 30 3b 0a 09  ats.inuse = 0;..
1df0: 67 63 5f 73 74 61 74 73 2e 74 6f 74 61 6c 20 3d  gc_stats.total =
1e00: 20 30 3b 0a 09 67 63 5f 73 74 61 74 73 2e 63 6c   0;..gc_stats.cl
1e10: 65 61 6e 65 64 20 3d 20 30 3b 0a 0a 23 69 66 64  eaned = 0;..#ifd
1e20: 65 66 20 44 45 42 55 47 0a 09 73 6c 61 62 5f 67  ef DEBUG..slab_g
1e30: 63 5f 64 75 6d 70 5f 74 79 70 65 73 28 29 3b 0a  c_dump_types();.
1e40: 23 65 6e 64 69 66 0a 0a 09 2f 2a 20 50 75 74 20  #endif.../* Put 
1e50: 74 68 65 20 72 6f 6f 74 73 20 69 6e 20 74 68 65  the roots in the
1e60: 20 71 75 65 75 65 20 2a 2f 0a 09 65 78 74 65 72   queue */..exter
1e70: 6e 20 63 68 61 72 20 67 63 72 6f 6f 74 5f 73 74  n char gcroot_st
1e80: 61 72 74 5b 5d 3b 0a 09 65 78 74 65 72 6e 20 63  art[];..extern c
1e90: 68 61 72 20 67 63 72 6f 6f 74 5f 65 6e 64 5b 5d  har gcroot_end[]
1ea0: 3b 0a 09 73 6c 61 62 5f 67 63 5f 6d 61 72 6b 5f  ;..slab_gc_mark_
1eb0: 72 61 6e 67 65 28 67 63 72 6f 6f 74 5f 73 74 61  range(gcroot_sta
1ec0: 72 74 2c 20 67 63 72 6f 6f 74 5f 65 6e 64 29 3b  rt, gcroot_end);
1ed0: 0a 7d 0a 0a 73 74 61 74 69 63 20 73 6c 61 62 5f  .}..static slab_
1ee0: 74 20 2a 20 73 6c 61 62 5f 67 65 74 28 76 6f 69  t * slab_get(voi
1ef0: 64 20 2a 20 70 29 0a 7b 0a 09 69 66 20 28 61 72  d * p).{..if (ar
1f00: 63 68 5f 69 73 5f 68 65 61 70 5f 70 6f 69 6e 74  ch_is_heap_point
1f10: 65 72 28 70 29 29 20 7b 0a 09 09 2f 2a 20 43 68  er(p)) {.../* Ch
1f20: 65 63 6b 20 6d 61 67 69 63 20 6e 75 6d 62 65 72  eck magic number
1f30: 73 20 2a 2f 0a 09 09 73 6c 61 62 5f 74 20 2a 20  s */...slab_t * 
1f40: 73 6c 61 62 20 3d 20 41 52 43 48 5f 50 41 47 45  slab = ARCH_PAGE
1f50: 5f 41 4c 49 47 4e 28 70 29 3b 0a 0a 09 09 69 66  _ALIGN(p);....if
1f60: 20 28 73 6c 61 62 5f 76 61 6c 69 64 28 73 6c 61   (slab_valid(sla
1f70: 62 2d 3e 74 79 70 65 29 20 26 26 20 73 6c 61 62  b->type) && slab
1f80: 2d 3e 6d 61 67 69 63 20 3d 3d 20 73 6c 61 62 2d  ->magic == slab-
1f90: 3e 74 79 70 65 2d 3e 6d 61 67 69 63 29 20 7b 0a  >type->magic) {.
1fa0: 09 09 09 72 65 74 75 72 6e 20 73 6c 61 62 3b 0a  ...return slab;.
1fb0: 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 20  ..}..}...return 
1fc0: 30 3b 0a 7d 0a 0a 76 6f 69 64 20 73 6c 61 62 5f  0;.}..void slab_
1fd0: 67 63 5f 6d 61 72 6b 28 76 6f 69 64 20 2a 20 72  gc_mark(void * r
1fe0: 6f 6f 74 29 0a 7b 0a 09 2f 2a 20 44 45 42 55 47  oot).{../* DEBUG
1ff0: 5f 54 52 41 50 5f 50 54 52 28 72 6f 6f 74 2c 20  _TRAP_PTR(root, 
2000: 6b 61 73 29 3b 20 2a 2f 0a 09 73 6c 61 62 5f 74  kas); */..slab_t
2010: 20 2a 20 73 6c 61 62 20 3d 20 73 6c 61 62 5f 67   * slab = slab_g
2020: 65 74 28 72 6f 6f 74 29 3b 0a 09 69 66 20 28 73  et(root);..if (s
2030: 6c 61 62 29 20 7b 0a 09 09 2f 2a 20 45 6e 74 72  lab) {.../* Entr
2040: 79 20 77 69 74 68 69 6e 20 74 68 65 20 73 6c 61  y within the sla
2050: 62 20 2a 2f 0a 09 09 69 6e 74 20 73 6c 6f 74 6e  b */...int slotn
2060: 75 6d 20 3d 20 53 4c 41 42 5f 53 4c 4f 54 5f 4e  um = SLAB_SLOT_N
2070: 55 4d 28 73 6c 61 62 2c 20 72 6f 6f 74 29 3b 0a  UM(slab, root);.
2080: 09 09 69 66 20 28 73 6c 6f 74 6e 75 6d 3e 3d 73  ..if (slotnum>=s
2090: 6c 61 62 2d 3e 74 79 70 65 2d 3e 63 6f 75 6e 74  lab->type->count
20a0: 29 20 7b 0a 09 09 09 2f 2a 20 4e 6f 74 20 61 20  ) {..../* Not a 
20b0: 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 20 2d 20  valid pointer - 
20c0: 64 6f 6e 27 74 20 6d 61 72 6b 20 2a 2f 0a 09 09  don't mark */...
20d0: 09 72 65 74 75 72 6e 3b 0a 09 09 7d 0a 09 09 61  .return;...}...a
20e0: 73 73 65 72 74 28 73 6c 6f 74 6e 75 6d 3c 73 6c  ssert(slotnum<sl
20f0: 61 62 2d 3e 74 79 70 65 2d 3e 63 6f 75 6e 74 29  ab->type->count)
2100: 3b 0a 0a 09 09 73 6c 61 62 5f 73 6c 6f 74 5f 74  ;....slab_slot_t
2110: 20 2a 20 73 6c 6f 74 20 3d 20 53 4c 41 42 5f 53   * slot = SLAB_S
2120: 4c 4f 54 28 73 6c 61 62 2c 20 73 6c 6f 74 6e 75  LOT(slab, slotnu
2130: 6d 29 3b 0a 09 09 69 66 20 28 28 76 6f 69 64 2a  m);...if ((void*
2140: 29 28 73 6c 6f 74 2b 31 29 20 3e 20 72 6f 6f 74  )(slot+1) > root
2150: 29 20 7b 0a 09 09 09 2f 2a 20 52 65 66 65 72 65  ) {..../* Refere
2160: 6e 63 65 20 74 6f 20 73 6c 6f 74 20 2d 20 69 67  nce to slot - ig
2170: 6e 6f 72 65 20 2a 2f 0a 09 09 7d 20 65 6c 73 65  nore */...} else
2180: 20 69 66 28 73 6c 6f 74 2d 3e 63 6f 6c 6f 75 72   if(slot->colour
2190: 20 3d 3d 20 67 63 77 68 69 74 65 29 20 7b 0a 09   == gcwhite) {..
21a0: 09 09 67 63 5f 73 74 61 74 73 2e 69 6e 75 73 65  ..gc_stats.inuse
21b0: 20 2b 3d 20 73 6c 61 62 2d 3e 74 79 70 65 2d 3e   += slab->type->
21c0: 65 73 69 7a 65 3b 0a 09 09 09 61 73 73 65 72 74  esize;....assert
21d0: 28 73 6c 6f 74 2d 3e 73 65 71 29 3b 0a 09 09 09  (slot->seq);....
21e0: 49 4e 54 45 52 52 55 50 54 5f 4d 4f 4e 49 54 4f  INTERRUPT_MONITO
21f0: 52 5f 41 55 54 4f 4c 4f 43 4b 28 6c 69 73 74 5f  R_AUTOLOCK(list_
2200: 6d 6f 76 65 5f 6c 6f 63 6b 29 20 7b 0a 09 09 09  move_lock) {....
2210: 09 4c 49 53 54 5f 44 45 4c 45 54 45 28 77 68 69  .LIST_DELETE(whi
2220: 74 65 2c 20 73 6c 6f 74 29 3b 0a 09 09 09 09 73  te, slot);.....s
2230: 6c 6f 74 2d 3e 63 6f 6c 6f 75 72 20 3d 20 67 63  lot->colour = gc
2240: 67 72 65 79 3b 0a 09 09 09 09 4c 49 53 54 5f 41  grey;.....LIST_A
2250: 50 50 45 4e 44 28 67 72 65 79 2c 20 73 6c 6f 74  PPEND(grey, slot
2260: 29 3b 0a 09 09 09 7d 0a 09 09 7d 0a 09 7d 0a 7d  );....}...}..}.}
2270: 0a 0a 76 6f 69 64 20 73 6c 61 62 5f 67 63 5f 6d  ..void slab_gc_m
2280: 61 72 6b 5f 72 61 6e 67 65 28 76 6f 69 64 20 2a  ark_range(void *
2290: 20 66 72 6f 6d 2c 20 76 6f 69 64 20 2a 20 74 6f   from, void * to
22a0: 29 0a 7b 0a 09 67 63 6c 65 76 65 6c 2b 2b 3b 0a  ).{..gclevel++;.
22b0: 09 69 66 20 28 67 63 6c 65 76 65 6c 3e 67 63 5f  .if (gclevel>gc_
22c0: 73 74 61 74 73 2e 6d 61 78 63 6f 6e 74 65 78 74  stats.maxcontext
22d0: 29 20 7b 0a 09 09 67 63 5f 73 74 61 74 73 2e 6d  ) {...gc_stats.m
22e0: 61 78 63 6f 6e 74 65 78 74 20 3d 20 67 63 6c 65  axcontext = gcle
22f0: 76 65 6c 3b 0a 09 7d 0a 09 63 6f 6e 74 65 78 74  vel;..}..context
2300: 5b 67 63 6c 65 76 65 6c 5d 2e 66 72 6f 6d 20 3d  [gclevel].from =
2310: 20 66 72 6f 6d 3b 0a 09 63 6f 6e 74 65 78 74 5b   from;..context[
2320: 67 63 6c 65 76 65 6c 5d 2e 74 6f 20 3d 20 74 6f  gclevel].to = to
2330: 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
2340: 20 73 6c 61 62 5f 67 63 5f 73 74 65 70 28 29 0a   slab_gc_step().
2350: 7b 0a 09 2f 2a 20 4d 61 72 6b 20 6e 6f 6e 2d 68  {../* Mark non-h
2360: 65 61 70 20 69 74 65 6d 73 20 2a 2f 0a 09 77 68  eap items */..wh
2370: 69 6c 65 28 67 63 6c 65 76 65 6c 29 20 7b 0a 09  ile(gclevel) {..
2380: 09 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 6e 65  ./* Check the ne
2390: 78 74 20 70 6f 69 6e 74 65 72 20 69 6e 20 74 68  xt pointer in th
23a0: 65 20 62 6c 6f 63 6b 20 2a 2f 0a 09 09 77 68 69  e block */...whi
23b0: 6c 65 28 63 6f 6e 74 65 78 74 5b 67 63 6c 65 76  le(context[gclev
23c0: 65 6c 5d 2e 66 72 6f 6d 20 26 26 20 63 6f 6e 74  el].from && cont
23d0: 65 78 74 5b 67 63 6c 65 76 65 6c 5d 2e 66 72 6f  ext[gclevel].fro
23e0: 6d 20 3c 20 63 6f 6e 74 65 78 74 5b 67 63 6c 65  m < context[gcle
23f0: 76 65 6c 5d 2e 74 6f 29 20 7b 0a 09 09 09 73 6c  vel].to) {....sl
2400: 61 62 5f 67 63 5f 6d 61 72 6b 28 2a 63 6f 6e 74  ab_gc_mark(*cont
2410: 65 78 74 5b 67 63 6c 65 76 65 6c 5d 2e 66 72 6f  ext[gclevel].fro
2420: 6d 2b 2b 29 3b 0a 09 09 7d 0a 09 09 63 6f 6e 74  m++);...}...cont
2430: 65 78 74 5b 67 63 6c 65 76 65 6c 5d 2e 66 72 6f  ext[gclevel].fro
2440: 6d 20 3d 20 63 6f 6e 74 65 78 74 5b 67 63 6c 65  m = context[gcle
2450: 76 65 6c 5d 2e 74 6f 20 3d 20 30 3b 0a 09 09 67  vel].to = 0;...g
2460: 63 6c 65 76 65 6c 2d 2d 3b 0a 09 7d 0a 0a 09 2f  clevel--;..}.../
2470: 2a 20 4d 61 72 6b 20 68 65 61 70 20 69 74 65 6d  * Mark heap item
2480: 73 20 2a 2f 0a 09 77 68 69 6c 65 28 67 72 65 79  s */..while(grey
2490: 20 26 26 20 21 67 63 6c 65 76 65 6c 29 20 7b 0a   && !gclevel) {.
24a0: 09 09 73 6c 61 62 5f 73 6c 6f 74 5f 74 20 2a 20  ..slab_slot_t * 
24b0: 63 6f 6e 73 74 20 73 6c 6f 74 20 3d 20 67 72 65  const slot = gre
24c0: 79 3b 0a 09 09 61 73 73 65 72 74 28 73 6c 6f 74  y;...assert(slot
24d0: 2d 3e 63 6f 6c 6f 75 72 20 3d 3d 20 67 63 67 72  ->colour == gcgr
24e0: 65 79 29 3b 0a 09 09 73 6c 61 62 5f 74 20 2a 20  ey);...slab_t * 
24f0: 63 6f 6e 73 74 20 73 6c 61 62 20 3d 20 41 52 43  const slab = ARC
2500: 48 5f 50 41 47 45 5f 41 4c 49 47 4e 28 73 6c 6f  H_PAGE_ALIGN(slo
2510: 74 29 3b 0a 09 09 69 66 20 28 73 6c 61 62 2d 3e  t);...if (slab->
2520: 74 79 70 65 2d 3e 6d 61 72 6b 29 20 7b 0a 09 09  type->mark) {...
2530: 09 2f 2a 20 54 79 70 65 20 73 70 65 63 69 66 69  ./* Type specifi
2540: 63 20 6d 61 72 6b 20 2a 2f 0a 09 09 09 73 6c 61  c mark */....sla
2550: 62 2d 3e 74 79 70 65 2d 3e 6d 61 72 6b 28 73 6c  b->type->mark(sl
2560: 6f 74 2b 31 29 3b 0a 09 09 7d 20 65 6c 73 65 20  ot+1);...} else 
2570: 7b 0a 09 09 09 2f 2a 20 47 65 6e 65 72 69 63 20  {..../* Generic 
2580: 6d 61 72 6b 20 2a 2f 0a 09 09 09 76 6f 69 64 20  mark */....void 
2590: 2a 2a 20 70 70 20 3d 20 28 76 6f 69 64 2a 2a 29  ** pp = (void**)
25a0: 28 73 6c 6f 74 2b 31 29 3b 0a 09 09 09 69 6e 74  (slot+1);....int
25b0: 20 70 63 6f 75 6e 74 20 3d 20 73 6c 61 62 2d 3e   pcount = slab->
25c0: 74 79 70 65 2d 3e 65 73 69 7a 65 2f 73 69 7a 65  type->esize/size
25d0: 6f 66 28 2a 70 70 29 3b 0a 09 09 09 66 6f 72 28  of(*pp);....for(
25e0: 69 6e 74 20 69 3d 30 3b 20 69 3c 70 63 6f 75 6e  int i=0; i<pcoun
25f0: 74 3b 20 69 2b 2b 29 20 7b 0a 09 09 09 09 73 6c  t; i++) {.....sl
2600: 61 62 5f 67 63 5f 6d 61 72 6b 28 70 70 5b 69 5d  ab_gc_mark(pp[i]
2610: 29 3b 0a 09 09 09 7d 0a 09 09 7d 0a 0a 09 09 49  );....}...}....I
2620: 4e 54 45 52 52 55 50 54 5f 4d 4f 4e 49 54 4f 52  NTERRUPT_MONITOR
2630: 5f 41 55 54 4f 4c 4f 43 4b 28 6c 69 73 74 5f 6d  _AUTOLOCK(list_m
2640: 6f 76 65 5f 6c 6f 63 6b 29 20 7b 0a 09 09 09 4c  ove_lock) {....L
2650: 49 53 54 5f 44 45 4c 45 54 45 28 67 72 65 79 2c  IST_DELETE(grey,
2660: 20 73 6c 6f 74 29 3b 0a 09 09 09 73 6c 6f 74 2d   slot);....slot-
2670: 3e 63 6f 6c 6f 75 72 20 3d 20 67 63 62 6c 61 63  >colour = gcblac
2680: 6b 3b 0a 09 09 09 4c 49 53 54 5f 41 50 50 45 4e  k;....LIST_APPEN
2690: 44 28 62 6c 61 63 6b 2c 20 73 6c 6f 74 29 3b 0a  D(black, slot);.
26a0: 09 09 7d 0a 09 7d 0a 7d 0a 0a 76 6f 69 64 20 73  ..}..}.}..void s
26b0: 6c 61 62 5f 67 63 28 29 0a 7b 0a 09 77 68 69 6c  lab_gc().{..whil
26c0: 65 28 67 63 6c 65 76 65 6c 20 7c 7c 20 67 72 65  e(gclevel || gre
26d0: 79 29 20 7b 0a 09 09 73 6c 61 62 5f 67 63 5f 73  y) {...slab_gc_s
26e0: 74 65 70 28 29 3b 0a 09 7d 0a 7d 0a 0a 76 6f 69  tep();..}.}..voi
26f0: 64 20 73 6c 61 62 5f 67 63 5f 6d 61 72 6b 5f 62  d slab_gc_mark_b
2700: 6c 6f 63 6b 28 76 6f 69 64 20 2a 2a 20 62 6c 6f  lock(void ** blo
2710: 63 6b 2c 20 73 69 7a 65 5f 74 20 73 69 7a 65 29  ck, size_t size)
2720: 0a 7b 0a 09 73 6c 61 62 5f 67 63 5f 6d 61 72 6b  .{..slab_gc_mark
2730: 5f 72 61 6e 67 65 28 62 6c 6f 63 6b 2c 20 62 6c  _range(block, bl
2740: 6f 63 6b 20 2b 20 73 69 7a 65 2f 73 69 7a 65 6f  ock + size/sizeo
2750: 66 28 2a 62 6c 6f 63 6b 29 29 3b 0a 7d 0a 0a 73  f(*block));.}..s
2760: 74 61 74 69 63 20 69 6e 74 20 73 6c 61 62 5f 67  tatic int slab_g
2770: 63 5f 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 73 77  c_incremental_sw
2780: 65 65 70 5f 6c 6f 63 6b 65 64 28 73 6c 61 62 5f  eep_locked(slab_
2790: 74 79 70 65 5f 74 20 2a 20 75 6e 74 69 6c 29 0a  type_t * until).
27a0: 7b 0a 09 69 66 20 28 21 67 61 72 62 61 67 65 29  {..if (!garbage)
27b0: 20 7b 0a 09 09 72 65 74 75 72 6e 20 30 3b 0a 09   {...return 0;..
27c0: 7d 0a 0a 09 77 68 69 6c 65 28 67 61 72 62 61 67  }...while(garbag
27d0: 65 29 20 7b 0a 09 09 73 6c 61 62 5f 73 6c 6f 74  e) {...slab_slot
27e0: 5f 74 20 2a 20 74 6f 66 72 65 65 20 3d 20 67 61  _t * tofree = ga
27f0: 72 62 61 67 65 3b 0a 09 09 61 73 73 65 72 74 28  rbage;...assert(
2800: 67 61 72 62 61 67 65 2d 3e 63 6f 6c 6f 75 72 20  garbage->colour 
2810: 3d 3d 20 67 63 77 68 69 74 65 29 3b 0a 09 09 73  == gcwhite);...s
2820: 6c 61 62 5f 74 20 2a 20 73 6c 61 62 20 3d 20 41  lab_t * slab = A
2830: 52 43 48 5f 50 41 47 45 5f 41 4c 49 47 4e 28 74  RCH_PAGE_ALIGN(t
2840: 6f 66 72 65 65 29 3b 0a 09 09 67 63 5f 73 74 61  ofree);...gc_sta
2850: 74 73 2e 63 6c 65 61 6e 65 64 20 2b 3d 20 73 6c  ts.cleaned += sl
2860: 61 62 2d 3e 74 79 70 65 2d 3e 65 73 69 7a 65 3b  ab->type->esize;
2870: 0a 09 09 69 66 20 28 73 6c 61 62 2d 3e 74 79 70  ...if (slab->typ
2880: 65 2d 3e 66 69 6e 61 6c 69 7a 65 29 20 7b 0a 09  e->finalize) {..
2890: 09 09 73 6c 61 62 2d 3e 74 79 70 65 2d 3e 66 69  ..slab->type->fi
28a0: 6e 61 6c 69 7a 65 28 74 6f 66 72 65 65 2b 31 29  nalize(tofree+1)
28b0: 3b 0a 09 09 7d 0a 09 09 49 4e 54 45 52 52 55 50  ;...}...INTERRUP
28c0: 54 5f 4d 4f 4e 49 54 4f 52 5f 41 55 54 4f 4c 4f  T_MONITOR_AUTOLO
28d0: 43 4b 28 6c 69 73 74 5f 6d 6f 76 65 5f 6c 6f 63  CK(list_move_loc
28e0: 6b 29 20 7b 0a 09 09 09 4c 49 53 54 5f 44 45 4c  k) {....LIST_DEL
28f0: 45 54 45 28 67 61 72 62 61 67 65 2c 20 74 6f 66  ETE(garbage, tof
2900: 72 65 65 29 3b 0a 09 09 09 73 6c 61 62 5f 73 6c  ree);....slab_sl
2910: 6f 74 5f 66 72 65 65 28 74 6f 66 72 65 65 29 3b  ot_free(tofree);
2920: 0a 23 69 66 64 65 66 20 44 45 42 55 47 0a 09 09  .#ifdef DEBUG...
2930: 09 73 6c 61 62 2d 3e 74 79 70 65 2d 3e 69 6e 75  .slab->type->inu
2940: 73 65 2d 2d 3b 0a 23 65 6e 64 69 66 0a 09 09 7d  se--;.#endif...}
2950: 0a 09 09 2f 2a 20 48 61 76 65 20 77 65 20 66 6f  .../* Have we fo
2960: 75 6e 64 20 77 68 61 74 20 77 65 27 72 65 20 6c  und what we're l
2970: 6f 6f 6b 69 6e 67 20 66 6f 72 3f 20 2a 2f 0a 09  ooking for? */..
2980: 09 69 66 20 28 75 6e 74 69 6c 20 26 26 20 75 6e  .if (until && un
2990: 74 69 6c 20 3d 3d 20 73 6c 61 62 2d 3e 74 79 70  til == slab->typ
29a0: 65 29 20 7b 0a 09 09 09 73 6c 61 62 2d 3e 74 79  e) {....slab->ty
29b0: 70 65 2d 3e 66 69 72 73 74 20 3d 20 73 6c 61 62  pe->first = slab
29c0: 3b 0a 09 09 09 72 65 74 75 72 6e 20 31 3b 0a 09  ;....return 1;..
29d0: 09 7d 20 65 6c 73 65 20 69 66 20 28 21 75 6e 74  .} else if (!unt
29e0: 69 6c 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 20  il) {....return 
29f0: 31 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 53  1;...}..}.../* S
2a00: 77 65 65 70 20 66 69 6e 69 73 68 65 64 2c 20 66  weep finished, f
2a10: 72 65 65 20 61 6e 79 20 70 61 67 65 73 20 6e 6f  ree any pages no
2a20: 77 20 65 6d 70 74 79 20 2a 2f 0a 09 73 6c 61 62  w empty */..slab
2a30: 5f 74 79 70 65 5f 74 20 2a 20 73 74 79 70 65 20  _type_t * stype 
2a40: 3d 20 74 79 70 65 73 3b 0a 09 77 68 69 6c 65 28  = types;..while(
2a50: 73 74 79 70 65 29 20 7b 0a 09 09 73 6c 61 62 5f  stype) {...slab_
2a60: 74 20 2a 20 73 6c 61 62 20 3d 20 73 74 79 70 65  t * slab = stype
2a70: 2d 3e 66 69 72 73 74 3b 0a 0a 09 09 77 68 69 6c  ->first;....whil
2a80: 65 28 73 6c 61 62 29 20 7b 0a 09 09 09 2f 2a 20  e(slab) {..../* 
2a90: 52 65 6c 65 61 73 65 20 70 61 67 65 20 69 66 20  Release page if 
2aa0: 6e 6f 77 20 65 6d 70 74 79 20 2a 2f 0a 09 09 09  now empty */....
2ab0: 49 4e 54 45 52 52 55 50 54 5f 4d 4f 4e 49 54 4f  INTERRUPT_MONITO
2ac0: 52 5f 41 55 54 4f 4c 4f 43 4b 28 6c 69 73 74 5f  R_AUTOLOCK(list_
2ad0: 6d 6f 76 65 5f 6c 6f 63 6b 29 20 7b 0a 09 09 09  move_lock) {....
2ae0: 09 69 66 20 28 73 6c 61 62 5f 61 6c 6c 5f 66 72  .if (slab_all_fr
2af0: 65 65 28 73 6c 61 62 29 29 20 7b 0a 09 09 09 09  ee(slab)) {.....
2b00: 09 73 6c 61 62 5f 74 20 2a 20 65 6d 70 74 79 20  .slab_t * empty 
2b10: 3d 20 73 6c 61 62 3b 0a 09 09 09 09 09 4c 49 53  = slab;......LIS
2b20: 54 5f 4e 45 58 54 28 73 74 79 70 65 2d 3e 66 69  T_NEXT(stype->fi
2b30: 72 73 74 2c 20 73 6c 61 62 29 3b 0a 09 09 09 09  rst, slab);.....
2b40: 09 4c 49 53 54 5f 44 45 4c 45 54 45 28 73 74 79  .LIST_DELETE(sty
2b50: 70 65 2d 3e 66 69 72 73 74 2c 20 65 6d 70 74 79  pe->first, empty
2b60: 29 3b 0a 09 09 09 09 09 70 61 67 65 5f 68 65 61  );......page_hea
2b70: 70 5f 66 72 65 65 28 65 6d 70 74 79 29 3b 0a 09  p_free(empty);..
2b80: 09 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09  ...} else {.....
2b90: 09 4c 49 53 54 5f 4e 45 58 54 28 73 74 79 70 65  .LIST_NEXT(stype
2ba0: 2d 3e 66 69 72 73 74 2c 20 73 6c 61 62 29 3b 0a  ->first, slab);.
2bb0: 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09 7d 0a 0a  ....}....}...}..
2bc0: 09 09 4c 49 53 54 5f 4e 45 58 54 28 74 79 70 65  ..LIST_NEXT(type
2bd0: 73 2c 20 73 74 79 70 65 29 3b 0a 09 7d 0a 09 72  s, stype);..}..r
2be0: 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 69 6e 74 20  eturn 0;.}..int 
2bf0: 73 6c 61 62 5f 67 63 5f 69 6e 63 72 65 6d 65 6e  slab_gc_incremen
2c00: 74 61 6c 5f 73 77 65 65 70 28 73 6c 61 62 5f 74  tal_sweep(slab_t
2c10: 79 70 65 5f 74 20 2a 20 75 6e 74 69 6c 29 0a 7b  ype_t * until).{
2c20: 0a 09 73 6c 61 62 5f 6c 6f 63 6b 28 30 29 3b 0a  ..slab_lock(0);.
2c30: 09 69 6e 74 20 72 65 74 76 61 6c 20 3d 20 73 6c  .int retval = sl
2c40: 61 62 5f 67 63 5f 69 6e 63 72 65 6d 65 6e 74 61  ab_gc_incrementa
2c50: 6c 5f 73 77 65 65 70 5f 6c 6f 63 6b 65 64 28 75  l_sweep_locked(u
2c60: 6e 74 69 6c 29 3b 0a 09 73 6c 61 62 5f 75 6e 6c  ntil);..slab_unl
2c70: 6f 63 6b 28 29 3b 0a 09 72 65 74 75 72 6e 20 72  ock();..return r
2c80: 65 74 76 61 6c 3b 0a 7d 0a 0a 76 6f 69 64 20 73  etval;.}..void s
2c90: 6c 61 62 5f 67 63 5f 65 6e 64 28 29 0a 7b 0a 09  lab_gc_end().{..
2ca0: 61 73 73 65 72 74 28 21 67 72 65 79 29 3b 0a 09  assert(!grey);..
2cb0: 69 66 20 28 67 63 5f 73 74 61 74 73 2e 69 6e 75  if (gc_stats.inu
2cc0: 73 65 20 3e 20 67 63 5f 73 74 61 74 73 2e 70 65  se > gc_stats.pe
2cd0: 61 6b 29 20 7b 0a 09 09 67 63 5f 73 74 61 74 73  ak) {...gc_stats
2ce0: 2e 70 65 61 6b 20 3d 20 67 63 5f 73 74 61 74 73  .peak = gc_stats
2cf0: 2e 69 6e 75 73 65 3b 0a 09 7d 0a 0a 09 2f 2a 20  .inuse;..}.../* 
2d00: 46 69 6e 61 6c 69 7a 65 20 67 61 72 62 61 67 65  Finalize garbage
2d10: 20 2a 2f 0a 09 67 61 72 62 61 67 65 20 3d 20 77   */..garbage = w
2d20: 68 69 74 65 3b 0a 09 77 68 69 74 65 20 3d 20 30  hite;..white = 0
2d30: 3b 0a 0a 09 2f 2a 20 4d 6f 76 65 20 65 76 65 72  ;.../* Move ever
2d40: 79 74 68 69 6e 67 20 62 61 63 6b 20 74 6f 20 74  ything back to t
2d50: 68 65 20 77 68 69 74 65 20 6c 69 73 74 20 2a 2f  he white list */
2d60: 0a 09 73 6c 61 62 5f 73 6c 6f 74 5f 74 20 2a 20  ..slab_slot_t * 
2d70: 73 6c 6f 74 20 3d 20 77 68 69 74 65 20 3d 20 62  slot = white = b
2d80: 6c 61 63 6b 3b 0a 09 77 68 69 6c 65 28 73 6c 6f  lack;..while(slo
2d90: 74 29 20 7b 0a 09 09 73 6c 6f 74 2d 3e 63 6f 6c  t) {...slot->col
2da0: 6f 75 72 20 3d 20 67 63 77 68 69 74 65 3b 0a 09  our = gcwhite;..
2db0: 09 4c 49 53 54 5f 4e 45 58 54 28 77 68 69 74 65  .LIST_NEXT(white
2dc0: 2c 20 73 6c 6f 74 29 3b 0a 09 7d 0a 09 62 6c 61  , slot);..}..bla
2dd0: 63 6b 20 3d 20 30 3b 0a 0a 09 73 6c 61 62 5f 75  ck = 0;...slab_u
2de0: 6e 6c 6f 63 6b 28 29 3b 0a 7d 0a 0a 73 74 61 74  nlock();.}..stat
2df0: 69 63 20 76 6f 69 64 20 73 6c 61 62 5f 74 65 73  ic void slab_tes
2e00: 74 5f 66 69 6e 61 6c 69 7a 65 28 76 6f 69 64 20  t_finalize(void 
2e10: 2a 20 70 29 0a 7b 0a 09 6b 65 72 6e 65 6c 5f 70  * p).{..kernel_p
2e20: 72 69 6e 74 6b 28 22 46 69 6e 61 6c 69 7a 69 6e  rintk("Finalizin
2e30: 67 3a 20 25 70 5c 6e 22 2c 20 70 29 3b 0a 7d 0a  g: %p\n", p);.}.
2e40: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6c 61  .static void sla
2e50: 62 5f 74 65 73 74 5f 6d 61 72 6b 28 76 6f 69 64  b_test_mark(void
2e60: 20 2a 70 29 0a 7b 0a 09 6b 65 72 6e 65 6c 5f 70   *p).{..kernel_p
2e70: 72 69 6e 74 6b 28 22 4d 61 72 6b 69 6e 67 3a 20  rintk("Marking: 
2e80: 25 70 5c 6e 22 2c 20 70 29 3b 0a 7d 0a 0a 73 74  %p\n", p);.}..st
2e90: 61 74 69 63 20 76 6f 69 64 20 73 6c 61 62 5f 77  atic void slab_w
2ea0: 65 61 6b 72 65 66 5f 6d 61 72 6b 28 76 6f 69 64  eakref_mark(void
2eb0: 20 2a 20 70 29 0a 7b 0a 09 73 6c 61 62 5f 77 65   * p).{..slab_we
2ec0: 61 6b 72 65 66 5f 74 20 2a 20 72 65 66 20 3d 20  akref_t * ref = 
2ed0: 28 73 6c 61 62 5f 77 65 61 6b 72 65 66 5f 74 2a  (slab_weakref_t*
2ee0: 29 70 3b 0a 09 69 66 20 28 72 65 66 2d 3e 63 68  )p;..if (ref->ch
2ef0: 61 6e 63 65 73 29 20 7b 0a 09 09 72 65 66 2d 3e  ances) {...ref->
2f00: 63 68 61 6e 63 65 73 2d 2d 3b 0a 09 09 73 6c 61  chances--;...sla
2f10: 62 5f 67 63 5f 6d 61 72 6b 28 72 65 66 2d 3e 70  b_gc_mark(ref->p
2f20: 29 3b 0a 09 7d 0a 7d 0a 0a 73 6c 61 62 5f 77 65  );..}.}..slab_we
2f30: 61 6b 72 65 66 5f 74 20 2a 20 73 6c 61 62 5f 77  akref_t * slab_w
2f40: 65 61 6b 72 65 66 28 76 6f 69 64 20 2a 20 70 29  eakref(void * p)
2f50: 0a 7b 0a 09 73 74 61 74 69 63 20 73 6c 61 62 5f  .{..static slab_
2f60: 74 79 70 65 5f 74 20 77 65 61 6b 72 65 66 73 5b  type_t weakrefs[
2f70: 5d 20 3d 20 7b 0a 09 09 53 4c 41 42 5f 54 59 50  ] = {...SLAB_TYP
2f80: 45 28 73 69 7a 65 6f 66 28 73 6c 61 62 5f 77 65  E(sizeof(slab_we
2f90: 61 6b 72 65 66 5f 74 29 2c 20 73 6c 61 62 5f 77  akref_t), slab_w
2fa0: 65 61 6b 72 65 66 5f 6d 61 72 6b 2c 20 30 29 2c  eakref_mark, 0),
2fb0: 0a 09 7d 3b 0a 09 73 74 61 74 69 63 20 73 6c 61  ..};..static sla
2fc0: 62 5f 77 65 61 6b 72 65 66 5f 74 20 6e 75 6c 6c  b_weakref_t null
2fd0: 72 65 66 5b 5d 20 3d 20 7b 30 7d 3b 0a 0a 09 73  ref[] = {0};...s
2fe0: 6c 61 62 5f 74 20 2a 20 73 6c 61 62 20 3d 20 73  lab_t * slab = s
2ff0: 6c 61 62 5f 67 65 74 28 70 29 3b 0a 09 69 66 20  lab_get(p);..if 
3000: 28 73 6c 61 62 29 20 7b 0a 09 09 73 6c 61 62 5f  (slab) {...slab_
3010: 77 65 61 6b 72 65 66 5f 74 20 2a 20 72 65 66 20  weakref_t * ref 
3020: 3d 20 73 6c 61 62 5f 61 6c 6c 6f 63 28 77 65 61  = slab_alloc(wea
3030: 6b 72 65 66 73 29 3b 0a 09 09 69 6e 74 20 73 6c  krefs);...int sl
3040: 6f 74 20 3d 20 53 4c 41 42 5f 53 4c 4f 54 5f 4e  ot = SLAB_SLOT_N
3050: 55 4d 28 73 6c 61 62 2c 20 70 29 3b 0a 09 09 72  UM(slab, p);...r
3060: 65 66 2d 3e 73 65 71 20 3d 20 53 4c 41 42 5f 53  ef->seq = SLAB_S
3070: 4c 4f 54 28 73 6c 61 62 2c 20 73 6c 6f 74 29 2d  LOT(slab, slot)-
3080: 3e 73 65 71 3b 0a 09 09 72 65 66 2d 3e 70 20 3d  >seq;...ref->p =
3090: 20 70 3b 0a 09 09 72 65 66 2d 3e 63 68 61 6e 63   p;...ref->chanc
30a0: 65 73 3d 30 3b 0a 0a 09 09 72 65 74 75 72 6e 20  es=0;....return 
30b0: 72 65 66 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09  ref;..} else {..
30c0: 09 73 6c 61 62 5f 77 65 61 6b 72 65 66 5f 74 20  .slab_weakref_t 
30d0: 2a 20 72 65 66 20 3d 20 73 6c 61 62 5f 61 6c 6c  * ref = slab_all
30e0: 6f 63 28 77 65 61 6b 72 65 66 73 29 3b 0a 09 09  oc(weakrefs);...
30f0: 72 65 66 2d 3e 70 20 3d 20 70 3b 0a 09 09 72 65  ref->p = p;...re
3100: 74 75 72 6e 20 72 65 66 3b 0a 09 7d 0a 7d 0a 0a  turn ref;..}.}..
3110: 76 6f 69 64 20 2a 20 73 6c 61 62 5f 77 65 61 6b  void * slab_weak
3120: 72 65 66 5f 67 65 74 28 73 6c 61 62 5f 77 65 61  ref_get(slab_wea
3130: 6b 72 65 66 5f 74 20 2a 20 72 65 66 29 0a 7b 0a  kref_t * ref).{.
3140: 09 73 6c 61 62 5f 74 20 2a 20 73 6c 61 62 20 3d  .slab_t * slab =
3150: 20 73 6c 61 62 5f 67 65 74 28 72 65 66 2d 3e 70   slab_get(ref->p
3160: 29 3b 0a 09 69 66 20 28 73 6c 61 62 29 20 7b 0a  );..if (slab) {.
3170: 09 09 69 6e 74 20 73 6c 6f 74 20 3d 20 53 4c 41  ..int slot = SLA
3180: 42 5f 53 4c 4f 54 5f 4e 55 4d 28 73 6c 61 62 2c  B_SLOT_NUM(slab,
3190: 20 72 65 66 2d 3e 70 29 3b 0a 09 09 69 66 20 28   ref->p);...if (
31a0: 72 65 66 2d 3e 73 65 71 20 3d 3d 20 53 4c 41 42  ref->seq == SLAB
31b0: 5f 53 4c 4f 54 28 73 6c 61 62 2c 20 73 6c 6f 74  _SLOT(slab, slot
31c0: 29 2d 3e 73 65 71 29 20 7b 0a 09 09 09 72 65 74  )->seq) {....ret
31d0: 75 72 6e 20 72 65 66 2d 3e 70 3b 0a 09 09 7d 20  urn ref->p;...} 
31e0: 65 6c 73 65 20 7b 0a 09 09 09 72 65 74 75 72 6e  else {....return
31f0: 20 30 3b 0a 09 09 7d 0a 09 7d 20 65 6c 73 65 20   0;...}..} else 
3200: 7b 0a 09 09 72 65 74 75 72 6e 20 72 65 66 2d 3e  {...return ref->
3210: 70 3b 0a 09 7d 0a 7d 0a 0a 23 64 65 66 69 6e 65  p;..}.}..#define
3220: 20 53 4c 41 42 5f 4d 41 58 5f 44 41 54 41 5f 41   SLAB_MAX_DATA_A
3230: 52 45 41 28 73 6c 6f 74 73 29 20 52 4f 55 4e 44  REA(slots) ROUND
3240: 44 4f 57 4e 28 28 28 28 41 52 43 48 5f 50 41 47  DOWN((((ARCH_PAG
3250: 45 5f 53 49 5a 45 2d 73 69 7a 65 6f 66 28 73 6c  E_SIZE-sizeof(sl
3260: 61 62 5f 74 29 2d 32 2a 73 69 7a 65 6f 66 28 75  ab_t)-2*sizeof(u
3270: 69 6e 74 33 32 5f 74 29 29 2f 73 6c 6f 74 73 29  int32_t))/slots)
3280: 2d 73 69 7a 65 6f 66 28 73 6c 61 62 5f 73 6c 6f  -sizeof(slab_slo
3290: 74 5f 74 29 29 2c 38 29 0a 73 74 61 74 69 63 20  t_t)),8).static 
32a0: 73 6c 61 62 5f 74 79 70 65 5f 74 20 70 6f 6f 6c  slab_type_t pool
32b0: 73 5b 5d 20 3d 20 7b 0a 23 69 66 20 30 0a 09 53  s[] = {.#if 0..S
32c0: 4c 41 42 5f 54 59 50 45 28 38 2c 20 30 2c 20 30  LAB_TYPE(8, 0, 0
32d0: 29 2c 0a 09 53 4c 41 42 5f 54 59 50 45 28 31 32  ),..SLAB_TYPE(12
32e0: 2c 20 30 2c 20 30 29 2c 0a 09 53 4c 41 42 5f 54  , 0, 0),..SLAB_T
32f0: 59 50 45 28 31 36 2c 20 30 2c 20 30 29 2c 0a 09  YPE(16, 0, 0),..
3300: 53 4c 41 42 5f 54 59 50 45 28 32 34 2c 20 30 2c  SLAB_TYPE(24, 0,
3310: 20 30 29 2c 0a 09 53 4c 41 42 5f 54 59 50 45 28   0),..SLAB_TYPE(
3320: 33 32 2c 20 30 2c 20 30 29 2c 0a 09 53 4c 41 42  32, 0, 0),..SLAB
3330: 5f 54 59 50 45 28 34 38 2c 20 30 2c 20 30 29 2c  _TYPE(48, 0, 0),
3340: 0a 09 53 4c 41 42 5f 54 59 50 45 28 36 34 2c 20  ..SLAB_TYPE(64, 
3350: 30 2c 20 30 29 2c 0a 09 53 4c 41 42 5f 54 59 50  0, 0),..SLAB_TYP
3360: 45 28 39 36 2c 20 30 2c 20 30 29 2c 0a 09 53 4c  E(96, 0, 0),..SL
3370: 41 42 5f 54 59 50 45 28 31 32 38 2c 20 30 2c 20  AB_TYPE(128, 0, 
3380: 30 29 2c 0a 09 53 4c 41 42 5f 54 59 50 45 28 31  0),..SLAB_TYPE(1
3390: 39 36 2c 20 30 2c 20 30 29 2c 0a 09 53 4c 41 42  96, 0, 0),..SLAB
33a0: 5f 54 59 50 45 28 32 35 36 2c 20 30 2c 20 30 29  _TYPE(256, 0, 0)
33b0: 2c 0a 09 53 4c 41 42 5f 54 59 50 45 28 33 38 34  ,..SLAB_TYPE(384
33c0: 2c 20 30 2c 20 30 29 2c 0a 09 53 4c 41 42 5f 54  , 0, 0),..SLAB_T
33d0: 59 50 45 28 35 31 32 2c 20 30 2c 20 30 29 2c 0a  YPE(512, 0, 0),.
33e0: 09 53 4c 41 42 5f 54 59 50 45 28 37 36 38 2c 20  .SLAB_TYPE(768, 
33f0: 30 2c 20 30 29 2c 0a 09 53 4c 41 42 5f 54 59 50  0, 0),..SLAB_TYP
3400: 45 28 31 30 32 34 2c 20 30 2c 20 30 29 2c 0a 23  E(1024, 0, 0),.#
3410: 65 6c 73 65 0a 09 53 4c 41 42 5f 54 59 50 45 28  else..SLAB_TYPE(
3420: 53 4c 41 42 5f 4d 41 58 5f 44 41 54 41 5f 41 52  SLAB_MAX_DATA_AR
3430: 45 41 28 31 32 38 29 2c 20 30 2c 20 30 29 2c 0a  EA(128), 0, 0),.
3440: 09 53 4c 41 42 5f 54 59 50 45 28 53 4c 41 42 5f  .SLAB_TYPE(SLAB_
3450: 4d 41 58 5f 44 41 54 41 5f 41 52 45 41 28 39 36  MAX_DATA_AREA(96
3460: 29 2c 20 30 2c 20 30 29 2c 0a 09 53 4c 41 42 5f  ), 0, 0),..SLAB_
3470: 54 59 50 45 28 53 4c 41 42 5f 4d 41 58 5f 44 41  TYPE(SLAB_MAX_DA
3480: 54 41 5f 41 52 45 41 28 37 38 29 2c 20 30 2c 20  TA_AREA(78), 0, 
3490: 30 29 2c 0a 09 53 4c 41 42 5f 54 59 50 45 28 53  0),..SLAB_TYPE(S
34a0: 4c 41 42 5f 4d 41 58 5f 44 41 54 41 5f 41 52 45  LAB_MAX_DATA_ARE
34b0: 41 28 36 34 29 2c 20 30 2c 20 30 29 2c 0a 09 53  A(64), 0, 0),..S
34c0: 4c 41 42 5f 54 59 50 45 28 53 4c 41 42 5f 4d 41  LAB_TYPE(SLAB_MA
34d0: 58 5f 44 41 54 41 5f 41 52 45 41 28 34 38 29 2c  X_DATA_AREA(48),
34e0: 20 30 2c 20 30 29 2c 0a 09 53 4c 41 42 5f 54 59   0, 0),..SLAB_TY
34f0: 50 45 28 53 4c 41 42 5f 4d 41 58 5f 44 41 54 41  PE(SLAB_MAX_DATA
3500: 5f 41 52 45 41 28 33 32 29 2c 20 30 2c 20 30 29  _AREA(32), 0, 0)
3510: 2c 0a 09 53 4c 41 42 5f 54 59 50 45 28 53 4c 41  ,..SLAB_TYPE(SLA
3520: 42 5f 4d 41 58 5f 44 41 54 41 5f 41 52 45 41 28  B_MAX_DATA_AREA(
3530: 32 34 29 2c 20 30 2c 20 30 29 2c 0a 09 53 4c 41  24), 0, 0),..SLA
3540: 42 5f 54 59 50 45 28 53 4c 41 42 5f 4d 41 58 5f  B_TYPE(SLAB_MAX_
3550: 44 41 54 41 5f 41 52 45 41 28 31 36 29 2c 20 30  DATA_AREA(16), 0
3560: 2c 20 30 29 2c 0a 09 53 4c 41 42 5f 54 59 50 45  , 0),..SLAB_TYPE
3570: 28 53 4c 41 42 5f 4d 41 58 5f 44 41 54 41 5f 41  (SLAB_MAX_DATA_A
3580: 52 45 41 28 31 32 29 2c 20 30 2c 20 30 29 2c 0a  REA(12), 0, 0),.
3590: 09 53 4c 41 42 5f 54 59 50 45 28 53 4c 41 42 5f  .SLAB_TYPE(SLAB_
35a0: 4d 41 58 5f 44 41 54 41 5f 41 52 45 41 28 38 29  MAX_DATA_AREA(8)
35b0: 2c 20 30 2c 20 30 29 2c 0a 09 53 4c 41 42 5f 54  , 0, 0),..SLAB_T
35c0: 59 50 45 28 53 4c 41 42 5f 4d 41 58 5f 44 41 54  YPE(SLAB_MAX_DAT
35d0: 41 5f 41 52 45 41 28 34 29 2c 20 30 2c 20 30 29  A_AREA(4), 0, 0)
35e0: 2c 0a 09 53 4c 41 42 5f 54 59 50 45 28 53 4c 41  ,..SLAB_TYPE(SLA
35f0: 42 5f 4d 41 58 5f 44 41 54 41 5f 41 52 45 41 28  B_MAX_DATA_AREA(
3600: 32 29 2c 20 30 2c 20 30 29 2c 0a 09 53 4c 41 42  2), 0, 0),..SLAB
3610: 5f 54 59 50 45 28 53 4c 41 42 5f 4d 41 58 5f 44  _TYPE(SLAB_MAX_D
3620: 41 54 41 5f 41 52 45 41 28 31 29 2c 20 30 2c 20  ATA_AREA(1), 0, 
3630: 30 29 2c 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 76  0),.#endif.};..v
3640: 6f 69 64 20 2a 20 73 6c 61 62 5f 6d 61 6c 6c 6f  oid * slab_mallo
3650: 63 28 73 69 7a 65 5f 74 20 73 69 7a 65 29 0a 7b  c(size_t size).{
3660: 0a 09 66 6f 72 28 69 6e 74 20 69 3d 30 3b 20 69  ..for(int i=0; i
3670: 3c 73 69 7a 65 6f 66 28 70 6f 6f 6c 73 29 2f 73  <sizeof(pools)/s
3680: 69 7a 65 6f 66 28 70 6f 6f 6c 73 5b 30 5d 29 3b  izeof(pools[0]);
3690: 69 2b 2b 29 20 7b 0a 09 09 69 66 20 28 70 6f 6f  i++) {...if (poo
36a0: 6c 73 5b 69 5d 2e 65 73 69 7a 65 20 3e 20 73 69  ls[i].esize > si
36b0: 7a 65 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 20  ze) {....return 
36c0: 73 6c 61 62 5f 61 6c 6c 6f 63 28 70 6f 6f 6c 73  slab_alloc(pools
36d0: 2b 69 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 4b 54  +i);...}..}...KT
36e0: 48 52 4f 57 46 28 41 6c 6c 6f 63 61 74 69 6f 6e  HROWF(Allocation
36f0: 54 6f 6f 42 69 67 45 78 63 65 70 74 69 6f 6e 2c  TooBigException,
3700: 20 22 41 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 6f   "Allocation too
3710: 20 62 69 67 20 66 6f 72 20 6d 61 6c 6c 6f 63 3a   big for malloc:
3720: 20 25 64 22 2c 20 73 69 7a 65 29 3b 0a 0a 09 2f   %d", size);.../
3730: 2a 20 4e 65 76 65 72 20 72 65 61 63 68 65 64 20  * Never reached 
3740: 2a 2f 0a 09 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  */..return 0;.}.
3750: 0a 76 6f 69 64 20 2a 20 73 6c 61 62 5f 72 65 61  .void * slab_rea
3760: 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 2c 20 73 69  lloc(void *p, si
3770: 7a 65 5f 74 20 73 69 7a 65 29 0a 7b 0a 09 69 66  ze_t size).{..if
3780: 20 28 30 20 3d 3d 20 70 29 20 7b 0a 09 09 72 65   (0 == p) {...re
3790: 74 75 72 6e 20 73 6c 61 62 5f 6d 61 6c 6c 6f 63  turn slab_malloc
37a0: 28 73 69 7a 65 29 3b 0a 09 7d 0a 0a 09 73 6c 61  (size);..}...sla
37b0: 62 5f 74 20 2a 20 73 6c 61 62 20 3d 20 73 6c 61  b_t * slab = sla
37c0: 62 5f 67 65 74 28 70 29 3b 0a 0a 09 69 66 20 28  b_get(p);...if (
37d0: 73 6c 61 62 29 20 7b 0a 09 09 69 66 20 28 73 69  slab) {...if (si
37e0: 7a 65 20 3c 3d 20 73 6c 61 62 2d 3e 74 79 70 65  ze <= slab->type
37f0: 2d 3e 65 73 69 7a 65 29 20 7b 0a 09 09 09 2f 2a  ->esize) {..../*
3800: 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 2c 20   Nothing to do, 
3810: 6e 65 77 20 6d 65 6d 6f 72 79 20 66 69 74 73 20  new memory fits 
3820: 69 6e 20 65 78 69 73 74 69 6e 67 20 73 6c 6f 74  in existing slot
3830: 20 2a 2f 0a 09 09 09 72 65 74 75 72 6e 20 70 3b   */....return p;
3840: 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 76  ...} else {....v
3850: 6f 69 64 20 2a 20 6e 65 77 20 3d 20 73 6c 61 62  oid * new = slab
3860: 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 29 3b 0a 0a  _malloc(size);..
3870: 09 09 09 2f 2a 20 43 6f 70 79 20 6f 6c 64 20 64  .../* Copy old d
3880: 61 74 61 20 28 6f 66 20 6f 6c 64 20 73 69 7a 65  ata (of old size
3890: 29 20 74 6f 20 6e 65 77 20 62 75 66 66 65 72 20  ) to new buffer 
38a0: 2a 2f 0a 09 09 09 72 65 74 75 72 6e 20 6d 65 6d  */....return mem
38b0: 63 70 79 28 6e 65 77 2c 20 70 2c 20 73 6c 61 62  cpy(new, p, slab
38c0: 2d 3e 74 79 70 65 2d 3e 65 73 69 7a 65 29 3b 0a  ->type->esize);.
38d0: 09 09 7d 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09  ..}..} else {...
38e0: 2f 2a 20 46 49 58 4d 45 3a 20 57 65 20 73 68 6f  /* FIXME: We sho
38f0: 75 6c 64 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67  uld do something
3900: 20 68 65 72 65 20 74 6f 20 77 61 72 6e 20 6f 66   here to warn of
3910: 20 6d 69 73 75 73 65 20 2a 2f 0a 09 09 6b 65 72   misuse */...ker
3920: 6e 65 6c 5f 70 61 6e 69 63 28 22 72 65 61 6c 6c  nel_panic("reall
3930: 6f 63 3a 20 49 6e 76 61 6c 69 64 20 68 65 61 70  oc: Invalid heap
3940: 20 70 6f 69 6e 74 65 72 3a 20 25 70 5c 6e 22 2c   pointer: %p\n",
3950: 20 70 29 3b 0a 09 7d 0a 0a 09 6b 65 72 6e 65 6c   p);..}...kernel
3960: 5f 70 61 6e 69 63 28 22 72 65 61 6c 6c 6f 63 3a  _panic("realloc:
3970: 20 77 65 20 73 68 6f 75 6c 64 6e 27 74 20 67 65   we shouldn't ge
3980: 74 20 68 65 72 65 21 22 29 3b 0a 09 72 65 74 75  t here!");..retu
3990: 72 6e 20 30 3b 0a 7d 0a 0a 76 6f 69 64 20 73 6c  rn 0;.}..void sl
39a0: 61 62 5f 74 65 73 74 28 29 0a 7b 0a 09 73 74 61  ab_test().{..sta
39b0: 74 69 63 20 73 6c 61 62 5f 74 79 70 65 5f 74 20  tic slab_type_t 
39c0: 74 5b 31 5d 20 3d 20 7b 53 4c 41 42 5f 54 59 50  t[1] = {SLAB_TYP
39d0: 45 28 31 32 37 30 2c 20 73 6c 61 62 5f 74 65 73  E(1270, slab_tes
39e0: 74 5f 6d 61 72 6b 2c 20 73 6c 61 62 5f 74 65 73  t_mark, slab_tes
39f0: 74 5f 66 69 6e 61 6c 69 7a 65 29 7d 3b 0a 09 76  t_finalize)};..v
3a00: 6f 69 64 20 2a 20 70 5b 34 5d 3b 0a 0a 09 70 5b  oid * p[4];...p[
3a10: 30 5d 20 3d 20 73 6c 61 62 5f 61 6c 6c 6f 63 28  0] = slab_alloc(
3a20: 74 29 3b 0a 09 70 5b 31 5d 20 3d 20 73 6c 61 62  t);..p[1] = slab
3a30: 5f 61 6c 6c 6f 63 28 74 29 3b 0a 09 70 5b 32 5d  _alloc(t);..p[2]
3a40: 20 3d 20 73 6c 61 62 5f 61 6c 6c 6f 63 28 74 29   = slab_alloc(t)
3a50: 3b 0a 09 70 5b 33 5d 20 3d 20 73 6c 61 62 5f 61  ;..p[3] = slab_a
3a60: 6c 6c 6f 63 28 74 29 3b 0a 0a 09 2f 2a 20 4e 6f  lloc(t);.../* No
3a70: 74 68 69 6e 67 20 73 68 6f 75 6c 64 20 62 65 20  thing should be 
3a80: 66 69 6e 61 6c 69 7a 65 64 20 68 65 72 65 20 2a  finalized here *
3a90: 2f 0a 09 74 68 72 65 61 64 5f 67 63 28 29 3b 0a  /..thread_gc();.
3aa0: 09 70 5b 30 5d 20 3d 20 70 5b 31 5d 20 3d 20 70  .p[0] = p[1] = p
3ab0: 5b 32 5d 20 3d 20 70 5b 33 5d 20 3d 20 6d 61 6c  [2] = p[3] = mal
3ac0: 6c 6f 63 28 36 35 33 29 3b 0a 0a 09 2f 2a 20 70  loc(653);.../* p
3ad0: 20 61 72 72 61 79 20 73 68 6f 75 6c 64 20 62 65   array should be
3ae0: 20 66 69 6e 61 6c 69 7a 65 64 20 68 65 72 65 20   finalized here 
3af0: 2a 2f 0a 09 74 68 72 65 61 64 5f 67 63 28 29 3b  */..thread_gc();
3b00: 0a 0a 09 70 5b 30 5d 20 3d 20 70 5b 31 5d 20 3d  ...p[0] = p[1] =
3b10: 20 70 5b 32 5d 20 3d 20 70 5b 33 5d 20 3d 20 72   p[2] = p[3] = r
3b20: 65 61 6c 6c 6f 63 28 70 5b 30 5d 2c 20 37 33 36  ealloc(p[0], 736
3b30: 29 3b 0a 09 70 5b 30 5d 20 3d 20 70 5b 31 5d 20  );..p[0] = p[1] 
3b40: 3d 20 70 5b 32 5d 20 3d 20 70 5b 33 5d 20 3d 20  = p[2] = p[3] = 
3b50: 72 65 61 6c 6c 6f 63 28 70 5b 30 5d 2c 20 31 37  realloc(p[0], 17
3b60: 33 36 29 3b 0a 0a 09 74 68 72 65 61 64 5f 67 63  36);...thread_gc
3b70: 28 29 3b 0a 7d 0a                                ();.}.