pmalloc.c File Reference

#include "common.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "machine.h"
#include "bu.h"
#include "vmath.h"
#include "bn.h"
#include "raytrace.h"

Include dependency graph for pmalloc.c:

Go to the source code of this file.

Data Structures

struct  overhead

Defines

#define NALIGN   sizeof(double)
#define CURBRK   sbrk(0)
#define BRK(x)   brk(x)
#define MAGIC_FREE   0x548a934c
#define MAGIC_BUSY   0xc139569a
#define TOADJ(p)   ((struct rt_qelem *)(p))
#define FROMADJ(p)   ((struct overhead *)(p))
#define FROMBUK(p)   ((struct overhead *)( (char *)p - sizeof(struct rt_qelem)))
#define TOBUK(p)   ((struct rt_qelem *)( (char *)p + sizeof(struct rt_qelem)))
#define debug   1
#define ASSERT(p, q)
#define NULL   0

Typedefs

typedef long Size

Functions

static void rt_pm_remque ()
char * rt_pmalloc (long nbytes, struct rt_pm_res *pmem)
void rt_pfree (char *mem, struct rt_pm_res *pmem)
char * rt_prealloc (char *mem, unsigned nbytes, struct rt_pm_res *pmem)
char * rt_pcalloc (unsigned num, unsigned size, struct rt_pm_res *pmem)
void rt_cfree (char *p, unsigned num, unsigned size, struct rt_pm_res *pmem)

Variables

char endfree
void(* mlabort )()
char endfree = 0
void(* mlabort )() = {0}


Detailed Description

The Princeton memory allocater.

Functions - rt_pmalloc rt_pfree rt_prealloc rt_forget

Author - Wiliam L. Sebok

Modified: John Anderson - modified for use in multi-threaded applications. It now uses a field from the BRL-CAD resource structure to store the memory blocks. Also replaced mlindx() routine with a faster (but less general) version that takes advantage of the fact that bucket sizes increase by powers of two.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * A "smarter" malloc v1.0 William L. Sebok Sept. 24, 1984 rev. June 30,1986 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Algorithm: Assign to each area an index "n". This is currently proportional to the log 2 of size of the area rounded down to the nearest integer. Then all free areas of storage whose length have the same index n are organized into a chain with other free areas of index n (the "bucket" chain). A request for allocation of storage first searches the list of free memory. The search starts at the bucket chain of index equal to that of the storage request, continuing to higher index bucket chains if the first attempt fails. If the search fails then new memory is allocated. Only the amount of new memory needed is allocated. Any old free memory left after an allocation is returned to the free list.

All memory areas (free or busy) handled by rt_pmalloc are also chained sequentially by increasing address (the adjacency chain). When memory is freed it is merged with adjacent free areas, if any. If a free area of memory ends at the end of memory (i.e. at the break), and if the variable "endfree" is non-zero, then the break is contracted, freeing the memory back to the system.

Notes: ov_length field includes sizeof(struct overhead) adjacency chain includes all memory, allocated plus free.

Definition in file pmalloc.c.


Define Documentation

#define NALIGN   sizeof(double)
 

Definition at line 95 of file pmalloc.c.

Referenced by rt_pmalloc(), and rt_prealloc().

#define CURBRK   sbrk(0)
 

Definition at line 108 of file pmalloc.c.

#define BRK x   )     brk(x)
 

Definition at line 123 of file pmalloc.c.

Referenced by rt_prealloc().

#define MAGIC_FREE   0x548a934c
 

Definition at line 128 of file pmalloc.c.

Referenced by rt_pfree(), rt_pmalloc(), and rt_prealloc().

#define MAGIC_BUSY   0xc139569a
 

Definition at line 129 of file pmalloc.c.

Referenced by rt_pfree(), and rt_prealloc().

#define TOADJ  )     ((struct rt_qelem *)(p))
 

Definition at line 150 of file pmalloc.c.

Referenced by rt_pfree(), and rt_prealloc().

#define FROMADJ  )     ((struct overhead *)(p))
 

Definition at line 151 of file pmalloc.c.

Referenced by rt_pfree(), and rt_prealloc().

#define FROMBUK  )     ((struct overhead *)( (char *)p - sizeof(struct rt_qelem)))
 

Definition at line 152 of file pmalloc.c.

Referenced by rt_pmalloc().

#define TOBUK  )     ((struct rt_qelem *)( (char *)p + sizeof(struct rt_qelem)))
 

Definition at line 153 of file pmalloc.c.

Referenced by rt_pfree(), and rt_prealloc().

#define debug   1
 

Definition at line 164 of file pmalloc.c.

Referenced by do_quad(), do_tri(), and nmg_radial_join_eu().

#define ASSERT p,
 ) 
 

Value:

if (!(p)) { \
                                bu_semaphore_acquire( BU_SEM_SYSCALL ); \
                                mllcerr(q); \
                                bu_semaphore_release( BU_SEM_SYSCALL ); \
                        }

Definition at line 166 of file pmalloc.c.

Referenced by rt_pfree(), and rt_pmalloc().

#define NULL   0
 

Definition at line 176 of file pmalloc.c.


Typedef Documentation

typedef long Size
 

Definition at line 98 of file pmalloc.c.


Function Documentation

static void rt_pm_remque  ) 
 

Referenced by rt_pfree(), rt_pmalloc(), and rt_prealloc().

char* rt_pmalloc long  nbytes,
struct rt_pm_res *  pmem
 

Definition at line 212 of file pmalloc.c.

References ASSERT, FROMBUK, MAGIC_FREE, NALIGN, overhead::ov_length, overhead::ov_magic, and rt_pm_remque().

Referenced by rt_pcalloc(), and rt_prealloc().

Here is the call graph for this function:

void rt_pfree char *  mem,
struct rt_pm_res *  pmem
 

Definition at line 393 of file pmalloc.c.

References ASSERT, endfree, FROMADJ, MAGIC_BUSY, MAGIC_FREE, NULL, overhead::ov_length, overhead::ov_magic, rt_pm_remque(), TOADJ, and TOBUK.

Referenced by rt_cfree(), and rt_prealloc().

Here is the call graph for this function:

char* rt_prealloc char *  mem,
unsigned  nbytes,
struct rt_pm_res *  pmem
 

Definition at line 508 of file pmalloc.c.

References bcopy, BRK, BU_SEM_SYSCALL, bu_semaphore_acquire(), bu_semaphore_release(), endfree, FROMADJ, length, MAGIC_BUSY, MAGIC_FREE, NALIGN, NULL, overhead::ov_length, overhead::ov_magic, rt_pfree(), rt_pm_remque(), rt_pmalloc(), TOADJ, TOBUK, and void().

Here is the call graph for this function:

char* rt_pcalloc unsigned  num,
unsigned  size,
struct rt_pm_res *  pmem
 

Definition at line 696 of file pmalloc.c.

References rt_pmalloc().

Here is the call graph for this function:

void rt_cfree char *  p,
unsigned  num,
unsigned  size,
struct rt_pm_res *  pmem
 

Definition at line 710 of file pmalloc.c.

References rt_pfree().

Here is the call graph for this function:


Variable Documentation

char endfree
 

Definition at line 183 of file pmalloc.c.

Referenced by rt_pfree(), and rt_prealloc().

void(* mlabort)()
 

Definition at line 209 of file pmalloc.c.

char endfree = 0
 

Definition at line 183 of file pmalloc.c.

Referenced by rt_pfree(), and rt_prealloc().

void(* mlabort)() = {0}
 

Definition at line 209 of file pmalloc.c.


Generated on Mon Sep 18 01:25:09 2006 for BRL-CAD by  doxygen 1.4.6