home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ixemul-45.0-src.tgz / tar.out / contrib / ixemul / library / kmalloc.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  103 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  kmalloc.c,v 1.1.1.1 1994/04/04 04:30:54 amiga Exp
  20.  *
  21.  *  kmalloc.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:30:54  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.3  1992/08/09  20:57:37  amiga
  26.  *  add declaration
  27.  *
  28.  *  Revision 1.2  1992/05/22  01:43:35  mwild
  29.  *  use buddy-alloc memory management
  30.  *
  31.  * Revision 1.1  1992/05/14  19:55:40  mwild
  32.  * Initial revision
  33.  *
  34.  */
  35.  
  36. #define _KERNEL
  37. #include "ixemul.h"
  38. #include "kprintf.h"
  39.  
  40. #include <exec/memory.h>
  41. #include <stdio.h>
  42.  
  43. /* This is a very simple and crude malloc package, only intended to
  44.    be used inside the library. We don't record what we allocated, as all
  45.    allocations are inside objects that are resource tracked, so no memory
  46.    should be lost (currently write buffers and memory files are allocated
  47.    with these functions) */
  48.  
  49. void *
  50. kmalloc (size_t size)
  51. {
  52.   u_int *res;
  53.  
  54.   /* always allocate a quantity of long words so we can CopyMemQuick() later */
  55.   size = (size + 3) & ~3;
  56.  
  57.   res = (u_int *) b_alloc(size + 4, MEMF_PUBLIC);
  58.   if (res) *res++ = size;
  59.  
  60.   return res;
  61. }
  62.  
  63. void *
  64. krealloc (void *mem, size_t size)
  65. {
  66.   u_int *res;
  67.  
  68.   if (! mem) return kmalloc (size);
  69.  
  70.   /* always allocate a quantity of long words */
  71.   size = (size + 3) & ~3;
  72.  
  73.   /* in that case the block is already large enough */
  74.   if (((u_int *)mem)[-1] >= size)
  75.     return mem;
  76.  
  77.   res = (u_int *) b_alloc(size + 4, MEMF_PUBLIC);
  78.   if (res)
  79.     {
  80.       *res++ = size;
  81.       CopyMemQuick (mem, res, ((u_int *)mem)[-1]);
  82.  
  83.       /* according to the manpage, the old buffer should only be
  84.        * freed, if the allocation of the new buffer was successful */
  85.       kfree (mem);
  86.     }
  87.  
  88.   return res;
  89. }
  90.  
  91. void
  92. kfree (void *mem)
  93. {
  94.   u_int *res;
  95.   
  96.   if (! mem) return;
  97.  
  98.   res = mem;
  99.   res--;
  100.  
  101.   b_free(res, *res + 4);
  102. }
  103.