00001 #include "slate.hpp"
00002
00003
00004
00005
00006 void memarea_module_init (struct object_heap* oh) {
00007 memset (oh->memory_areas, 0, sizeof (oh->memory_areas));
00008 memset (oh->memory_sizes, 0, sizeof (oh->memory_sizes));
00009 memset (oh->memory_num_refs, 0, sizeof (oh->memory_num_refs));
00010 }
00011
00012 int memarea_handle_isvalid (struct object_heap* oh, int memory) {
00013 return (oh->memory_areas [memory] != NULL);
00014 }
00015
00016 int memarea_allocate(struct object_heap* oh)
00017 {
00018 int memory;
00019 for (memory = 0; memory < SLATE_MEMS_MAXIMUM; ++ memory) {
00020 if (!(memarea_handle_isvalid(oh, memory)))
00021 return memory;
00022 }
00023 return SLATE_ERROR_RETURN;
00024 }
00025
00026 void memarea_close (struct object_heap* oh, int memory) {
00027 if (memarea_handle_isvalid(oh, memory)) {
00028 if (!--oh->memory_num_refs [memory]) {
00029 free (oh->memory_areas [memory]);
00030 oh->memory_areas [memory] = NULL;
00031 oh->memory_sizes [memory] = 0;
00032 }
00033 }
00034 }
00035
00036 void memarea_addref (struct object_heap* oh, int memory) {
00037 if (memarea_handle_isvalid(oh, memory))
00038 ++oh->memory_num_refs [memory];
00039 }
00040
00041 int memarea_open (struct object_heap* oh, int size) {
00042 void* area;
00043 int memory;
00044 memory = memarea_allocate (oh);
00045 if (memory < 0)
00046 return SLATE_ERROR_RETURN;
00047 area = malloc (size);
00048 if (area == NULL) {
00049 memarea_close (oh, memory);
00050 return SLATE_ERROR_RETURN;
00051 } else {
00052 oh->memory_areas [memory] = area;
00053 oh->memory_sizes [memory] = size;
00054 oh->memory_num_refs [memory] = 1;
00055 return memory;
00056 }
00057 }
00058
00059 int memarea_write (struct object_heap* oh, int memory, int memStart, int n, byte_t* bytes) {
00060 void* area;
00061 int nDelimited;
00062 if (!(memarea_handle_isvalid(oh, memory)))
00063 return SLATE_ERROR_RETURN;
00064 area = oh->memory_areas [memory];
00065 nDelimited = oh->memory_sizes [memory] - memStart;
00066 if (nDelimited > n)
00067 nDelimited = n;
00068 if (nDelimited > 0)
00069 memcpy (bytes, area, nDelimited);
00070 return nDelimited;
00071 }
00072
00073 int memarea_read (struct object_heap* oh, int memory, int memStart, int n, byte_t* bytes) {
00074 void* area;
00075 int nDelimited;
00076 if (!(memarea_handle_isvalid(oh, memory)))
00077 return SLATE_ERROR_RETURN;
00078 area = oh->memory_areas [memory];
00079 nDelimited = oh->memory_sizes [memory] - memStart;
00080 if (nDelimited > n)
00081 nDelimited = n;
00082 if (nDelimited > 0)
00083 memcpy (area, bytes, nDelimited);
00084 return nDelimited;
00085 }
00086
00087 int memarea_sizeof (struct object_heap* oh, int memory) {
00088 return (memarea_handle_isvalid(oh, memory) ? oh->memory_sizes [memory] : SLATE_ERROR_RETURN);
00089 }
00090
00091 int memarea_resize (struct object_heap* oh, int memory, int size) {
00092 void* result;
00093 if (!(memarea_handle_isvalid(oh, memory)))
00094 return SLATE_ERROR_RETURN;
00095 if (oh->memory_num_refs [memory] != 1)
00096 return SLATE_ERROR_RETURN;
00097 if (oh->memory_sizes [memory] == size)
00098 return size;
00099 result = realloc (oh->memory_areas [memory], size);
00100 if (result == NULL)
00101 return SLATE_ERROR_RETURN;
00102 oh->memory_areas [memory] = result;
00103 oh->memory_sizes [memory] = size;
00104 return size;
00105 }
00106
00107 int memarea_addressof (struct object_heap* oh, int memory, int offset, byte_t* addressBuffer) {
00108 void* result;
00109 result = ((memarea_handle_isvalid(oh, memory)
00110 && 0 <= offset && offset < oh->memory_sizes [memory])
00111 ? (char *) oh->memory_areas [memory] + offset : NULL);
00112 memcpy (addressBuffer, (char *) &result, sizeof (&result));
00113 return sizeof (result);
00114 }