Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
  4.  *
  5.  * Permission is hereby granted, free of charge, to any person obtaining a
  6.  * copy of this software and associated documentation files (the "Software"),
  7.  * to deal in the Software without restriction, including without limitation
  8.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9.  * and/or sell copies of the Software, and to permit persons to whom the
  10.  * Software is furnished to do so, subject to the following conditions:
  11.  *
  12.  * The above copyright notice and this permission notice shall be included
  13.  * in all copies or substantial portions of the Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21.  * OTHER DEALINGS IN THE SOFTWARE.
  22.  *
  23.  **************************************************************************/
  24.  
  25.  
  26. /**
  27.  * \file exemem.c
  28.  * Functions for allocating executable memory.
  29.  *
  30.  * \author Keith Whitwell
  31.  */
  32.  
  33.  
  34. #include "pipe/p_compiler.h"
  35. #include "util/u_debug.h"
  36. #include "os/os_thread.h"
  37. #include "util/u_memory.h"
  38.  
  39. #include "rtasm_execmem.h"
  40.  
  41. #include "util/u_mm.h"
  42.  
  43. #define EXEC_HEAP_SIZE (4*1024*1024)
  44.  
  45. pipe_static_mutex(exec_mutex);
  46.  
  47. static struct mem_block *exec_heap = NULL;
  48. static unsigned char *exec_mem = NULL;
  49.  
  50.  
  51. static void
  52. init_heap(void)
  53. {
  54.    if (!exec_heap)
  55.       exec_heap = u_mmInit( 0, EXEC_HEAP_SIZE );
  56.    
  57.    if (!exec_mem)
  58.       exec_mem = (unsigned char *) user_alloc(EXEC_HEAP_SIZE);
  59. }
  60.  
  61.  
  62. void *
  63. rtasm_exec_malloc(size_t size)
  64. {
  65.    struct mem_block *block = NULL;
  66.    void *addr = NULL;
  67.  
  68.    pipe_mutex_lock(exec_mutex);
  69.  
  70.    init_heap();
  71.  
  72.    if (exec_heap) {
  73.       size = (size + 31) & ~31;  /* next multiple of 32 bytes */
  74.       block = u_mmAllocMem( exec_heap, size, 5, 0 ); /* 5 -> 32-byte alignment */
  75.    }
  76.  
  77.    if (block)
  78.       addr = exec_mem + block->ofs;
  79.    else
  80.       debug_printf("rtasm_exec_malloc failed\n");
  81.    
  82.    pipe_mutex_unlock(exec_mutex);
  83.    
  84.    return addr;
  85. }
  86.  
  87.  
  88. void
  89. rtasm_exec_free(void *addr)
  90. {
  91.    pipe_mutex_lock(exec_mutex);
  92.  
  93.    if (exec_heap) {
  94.       struct mem_block *block = u_mmFindBlock(exec_heap, (unsigned char *)addr - exec_mem);
  95.    
  96.       if (block)
  97.          u_mmFreeMem(block);
  98.    }
  99.  
  100.    pipe_mutex_unlock(exec_mutex);
  101. }
  102.  
  103.