source: proiecte/swift/trunk/src/swift_allocator.c @ 176

Last change on this file since 176 was 176, checked in by (none), 14 years ago
  • imported repo from "guagal"
File size: 2.1 KB
Line 
1/*
2 * swift_allocator.c
3 *
4 * (c) 2009 Ionut Rosoiu <ionut.rosoiu@gmail.com>
5 *
6 */
7
8#include <stdlib.h>
9#include <stdio.h>
10#include "swift_assert.h"
11#include "swift_allocator.h"
12#include "swift_log.h"
13
14inline
15void* swift_malloc_system(size_t size) {
16        void *ret = malloc(size);
17
18        swift_assert(ret);
19
20        return ret;
21}
22
23inline
24void swift_free_system(void *addr) {
25        free(addr);
26}
27
28inline
29void* swift_malloc(swift_thread_t *thread, size_t size) {
30        //TODO: check
31        void *ret;
32
33#ifdef SWIFT_USE_CUSTOM_ALLOCATOR
34        swift_allocator_list_t **b, *elem;
35        size_t sz;
36
37        /* allocate space for the allocator's list */
38        size += sizeof(swift_allocator_list_t);
39        size = SWIFT_MEM_ALIGN(size);
40
41        for (b = thread->buckets, sz = SWIFT_MALLOC_ALIGN;
42                 sz < size;
43                 sz <<= 1, b++) {
44        }
45
46        elem = *b;
47        if (elem) {
48                /* reuse from the bucket */
49                *b = elem->next;
50                SWIFT_LOG(INFO, "get %d from bucket %p  --> elem=%p\n", sz, (void*)b, (void*) elem);
51
52        } else {
53                /* get from the heap */
54                elem = thread->heap_top;
55                thread->heap_top = ((char*)thread->heap_top + SWIFT_MEM_ALIGN(sz));
56
57                SWIFT_LOG(INFO, "get %d from heap for bucket %p --> elem=%p\n", sz, (void*)b,  (void*)elem);
58        }
59
60        /* store from what bucket this came from */
61        elem->next = b;
62
63        swift_assert((void*)elem < thread->heap_limit);
64
65        ret = elem;
66        ret = (void*) ((char*)ret + SWIFT_MEM_ALIGN(sizeof(swift_allocator_list_t)));
67
68        SWIFT_LOG(INFO, "malloc return %p\n", ret);
69#else
70        ret = malloc(size);
71#endif
72
73        return ret;
74}
75
76inline
77void swift_free(swift_thread_t *thread, void *addr) {
78#ifdef SWIFT_USE_CUSTOM_ALLOCATOR
79        swift_allocator_list_t *elem;
80        swift_allocator_list_t **b;
81
82        elem = (swift_allocator_list_t*) ((char*)addr - SWIFT_MEM_ALIGN(sizeof(swift_allocator_list_t)));
83
84        /* get the bucket this element came from */
85        b = elem->next;
86
87        SWIFT_LOG(INFO, "return addr=%p to bucket %p (*b=%p) --> elem=%p\n", addr, (void*)b, (void*)*b, (void*)elem);
88
89        /* return to the bucket */
90        elem->next = *b;
91        *b = elem;
92#else
93        //free(addr);
94#endif
95}
96
97inline
98void swift_retire_frame(swift_thread_t *thread, swift_frame_t *frame) {
99        /* drop into the pool */
100        swift_free(thread, frame);
101}
Note: See TracBrowser for help on using the repository browser.