home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / library / rcs / kmalloc.c,v < prev    next >
Encoding:
Text File  |  1992-08-09  |  3.4 KB  |  181 lines

  1. head    1.3;
  2. access;
  3. symbols
  4.     version39-41:1.2;
  5. locks;
  6. comment    @ *  @;
  7.  
  8.  
  9. 1.3
  10. date    92.08.09.20.57.37;    author amiga;    state Exp;
  11. branches;
  12. next    1.2;
  13.  
  14. 1.2
  15. date    92.05.22.01.43.35;    author mwild;    state Exp;
  16. branches;
  17. next    1.1;
  18.  
  19. 1.1
  20. date    92.05.14.19.55.40;    author mwild;    state Exp;
  21. branches;
  22. next    ;
  23.  
  24.  
  25. desc
  26. @global (not task-private) allocator, uses MEMF_PUBLIC
  27. @
  28.  
  29.  
  30. 1.3
  31. log
  32. @add declaration
  33. @
  34. text
  35. @/*
  36.  *  This file is part of ixemul.library for the Amiga.
  37.  *  Copyright (C) 1991, 1992  Markus M. Wild
  38.  *
  39.  *  This library is free software; you can redistribute it and/or
  40.  *  modify it under the terms of the GNU Library General Public
  41.  *  License as published by the Free Software Foundation; either
  42.  *  version 2 of the License, or (at your option) any later version.
  43.  *
  44.  *  This library is distributed in the hope that it will be useful,
  45.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  46.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  47.  *  Library General Public License for more details.
  48.  *
  49.  *  You should have received a copy of the GNU Library General Public
  50.  *  License along with this library; if not, write to the Free
  51.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  52.  *
  53.  *  $Id: kmalloc.c,v 1.2 1992/05/22 01:43:35 mwild Exp $
  54.  *
  55.  *  $Log: kmalloc.c,v $
  56.  *  Revision 1.2  1992/05/22  01:43:35  mwild
  57.  *  use buddy-alloc memory management
  58.  *
  59.  * Revision 1.1  1992/05/14  19:55:40  mwild
  60.  * Initial revision
  61.  *
  62.  */
  63.  
  64. #define KERNEL
  65. #include "ixemul.h"
  66. #include <exec/memory.h>
  67.  
  68. #undef DEBUG
  69.  
  70. #ifdef DEBUG
  71. #define DP(a) kprintf a
  72. #else
  73. #define DP(a)
  74. #endif
  75.  
  76. #ifndef BARE_ALLOCMEM
  77. #define AllocMem(size,attr)    b_alloc(size,attr)
  78. #define FreeMem(buf,size)    b_free(buf,size)
  79. void *b_alloc(int,int);
  80. void b_free(void *,int);
  81. #endif
  82.  
  83. void kfree (void *);
  84.  
  85. /* This is a very simple and crude malloc package, only intended to
  86.    be used inside the library. We don't record what we allocated, as all
  87.    allocations are inside objects that are resource tracked, so no memory
  88.    should be lost (currently write buffers and memory files are allocated
  89.    with these functions) */
  90.  
  91.  
  92. void *
  93. kmalloc (size_t size)
  94. {
  95.   u_int *res;
  96.  
  97.   /* always allocate a quantity of long words so we can CopyMemQuick() later */
  98.   size = (size + 3) & ~3;
  99.  
  100.   res = (u_int *) AllocMem (size + 4, MEMF_PUBLIC);
  101.   if (res) *res++ = size;
  102.  
  103. DP(("kmalloc (%ld) = $%lx.\n", size, res));
  104.  
  105.   return res;
  106. }
  107.  
  108. void *
  109. krealloc (void *mem, size_t size)
  110. {
  111.   u_int *res;
  112.  
  113.   if (! mem) return kmalloc (size);
  114.  
  115.   /* always allocate a quantity of long words */
  116.   size = (size + 3) & ~3;
  117.  
  118.   /* in that case the block is already large enough */
  119.   if (((u_int *)mem)[-1] >= size)
  120.     return mem;
  121.  
  122.   res = (u_int *) AllocMem (size + 4, MEMF_PUBLIC);
  123.   if (res)
  124.     {
  125.       *res++ = size;
  126.       CopyMemQuick (mem, res, ((u_int *)mem)[-1]);
  127.  
  128.       /* according to the manpage, the old buffer should only be
  129.        * freed, if the allocation of the new buffer was successful */
  130.       kfree (mem);
  131.     }
  132.  
  133. DP(("krealloc ($%lx, %ld) = $%lx.\n", mem, size, res));
  134.  
  135.   return res;
  136. }
  137.  
  138. void
  139. kfree (void *mem)
  140. {
  141.   u_int *res;
  142.   
  143.   if (! mem) return;
  144.  
  145.   res = mem;
  146.   res--;
  147.  
  148. DP(("kfree ($%lx), size = %ld.\n", res+1, *res));
  149.   
  150.   FreeMem (res, *res + 4);
  151. }
  152. @
  153.  
  154.  
  155. 1.2
  156. log
  157. @use buddy-alloc memory management
  158. @
  159. text
  160. @d19 1
  161. a19 1
  162.  *  $Id: kmalloc.c,v 1.1 1992/05/14 19:55:40 mwild Exp $
  163. d22 3
  164. d49 1
  165. @
  166.  
  167.  
  168. 1.1
  169. log
  170. @Initial revision
  171. @
  172. text
  173. @d19 1
  174. a19 1
  175.  *  $Id$
  176. d21 4
  177. a24 1
  178.  *  $Log$
  179. d38 8
  180. @
  181.