Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

malloc.c

Go to the documentation of this file.
00001 /****************************************************************
00002 Copyright 1990, 1994 by AT&T, Lucent Technologies and Bellcore.
00003 
00004 Permission to use, copy, modify, and distribute this software
00005 and its documentation for any purpose and without fee is hereby
00006 granted, provided that the above copyright notice appear in all
00007 copies and that both that the copyright notice and this
00008 permission notice and warranty disclaimer appear in supporting
00009 documentation, and that the names of AT&T, Bell Laboratories,
00010 Lucent or Bellcore or any of their entities not be used in
00011 advertising or publicity pertaining to distribution of the
00012 software without specific, written prior permission.
00013 
00014 AT&T, Lucent and Bellcore disclaim all warranties with regard to
00015 this software, including all implied warranties of
00016 merchantability and fitness.  In no event shall AT&T, Lucent or
00017 Bellcore be liable for any special, indirect or consequential
00018 damages or any damages whatsoever resulting from loss of use,
00019 data or profits, whether in an action of contract, negligence or
00020 other tortious action, arising out of or in connection with the
00021 use or performance of this software.
00022 ****************************************************************/
00023 
00024 #ifndef CRAY
00025 #define STACKMIN 512
00026 #define MINBLK (2*sizeof(struct mem) + 16)
00027 #define F _malloc_free_
00028 #define SBGULP 8192
00029 #include "string.h"     /* for memcpy */
00030 
00031 #ifdef KR_headers
00032 #define Char char
00033 #define Unsigned unsigned
00034 #define Int /*int*/
00035 #else
00036 #define Char void
00037 #define Unsigned size_t
00038 #define Int int
00039 #endif
00040 
00041 typedef struct mem {
00042         struct mem *next;
00043         Unsigned len;
00044         } mem;
00045 
00046 mem *F;
00047 
00048  Char *
00049 #ifdef KR_headers
00050 malloc(size)
00051         register Unsigned size;
00052 #else
00053 malloc(register Unsigned size)
00054 #endif
00055 {
00056         register mem *p, *q, *r, *s;
00057         unsigned register k, m;
00058         extern Char *sbrk(Int);
00059         char *top, *top1;
00060 
00061         size = (size+7) & ~7;
00062         r = (mem *) &F;
00063         for (p = F, q = 0; p; r = p, p = p->next) {
00064                 if ((k = p->len) >= size && (!q || m > k)) {
00065                         m = k;
00066                         q = p;
00067                         s = r;
00068                         }
00069                 }
00070         if (q) {
00071                 if (q->len - size >= MINBLK) { /* split block */
00072                         p = (mem *) (((char *) (q+1)) + size);
00073                         p->next = q->next;
00074                         p->len = q->len - size - sizeof(mem);
00075                         s->next = p;
00076                         q->len = size;
00077                         }
00078                 else
00079                         s->next = q->next;
00080                 }
00081         else {
00082                 top = (Char *)(((long)sbrk(0) + 7) & ~7);
00083                 if (F && (char *)(F+1) + F->len == top) {
00084                         q = F;
00085                         F = F->next;
00086                         }
00087                 else
00088                         q = (mem *) top;
00089                 top1 = (char *)(q+1) + size;
00090                 if (sbrk((int)(top1-top+SBGULP)) == (Char *) -1)
00091                         return 0;
00092                 r = (mem *)top1;
00093                 r->len = SBGULP - sizeof(mem);
00094                 r->next = F;
00095                 F = r;
00096                 q->len = size;
00097                 }
00098         return (Char *) (q+1);
00099         }
00100 
00101  void
00102 #ifdef KR_headers
00103 free(f)
00104         Char *f;
00105 #else
00106 free(Char *f)
00107 #endif
00108 {
00109         mem *p, *q, *r;
00110         char *pn, *qn;
00111 
00112         if (!f) return;
00113         q = (mem *) ((char *)f - sizeof(mem));
00114         qn = (char *)f + q->len;
00115         for (p = F, r = (mem *) &F; ; r = p, p = p->next) {
00116                 if (qn == (Char *) p) {
00117                         q->len += p->len + sizeof(mem);
00118                         p = p->next;
00119                         }
00120                 pn = p ? ((char *) (p+1)) + p->len : 0;
00121                 if (pn == (Char *) q) {
00122                         p->len += sizeof(mem) + q->len;
00123                         q->len = 0;
00124                         q->next = p;
00125                         r->next = p;
00126                         break;
00127                         }
00128                 if (pn < (char *) q) {
00129                         r->next = q;
00130                         q->next = p;
00131                         break;
00132                         }
00133                 }
00134         }
00135 
00136  Char *
00137 #ifdef KR_headers
00138 realloc(f, size)
00139         Char *f;
00140         Unsigned size;
00141 #else
00142 realloc(Char *f, Unsigned size)
00143 #endif
00144 {
00145         mem *p;
00146         Char *q, *f1;
00147         Unsigned s1;
00148 
00149         if (!f) return malloc(size);
00150         p = (mem *) ((char *)f - sizeof(mem));
00151         s1 = p->len;
00152         free(f);
00153         if (s1 > size)
00154                 s1 = size + 7 & ~7;
00155         if (!p->len) {
00156                 f1 = (Char *)(p->next + 1);
00157                 memcpy(f1, f, s1);
00158                 f = f1;
00159                 }
00160         q = malloc(size);
00161         if (q && q != f)
00162                 memcpy(q, f, s1);
00163         return q;
00164         }
00165 #endif
 

Powered by Plone

This site conforms to the following standards: