Beispiel #1
0
NOINLINE static void js_gc_mark()
{
    uint32_t stack_dummy;
    intptr_t** ptrptr = (intptr_t**)stack_top;
    alloc_t* alloc;
    global_t* g;
    while((intptr_t)ptrptr > (intptr_t)&stack_dummy) {
        if(((intptr_t)*ptrptr & 3) == 0) {
            intptr_t p = (intptr_t)*ptrptr;
            if(sizeof(intptr_t) == 8) {
                p &= 0x7ffffffffffful;
            }
            alloc = allocs_lookup((intptr_t*)p);
            if(alloc) {
                js_gc_mark_allocation(alloc);
            }
        }
        ptrptr--;
    }
    for(g = globals; g; g = g->next) {
        uint32_t i;
        for(i = 0; i < g->size; i++) {
            alloc = allocs_lookup(g->ptr[i]);
            if(alloc) {
                js_gc_mark_allocation(alloc);
            }
        }
    }
}
Beispiel #2
0
Datei: gc.c Projekt: filp/jsos
void* js_alloc_no_pointer(size_t sz)
#endif
{
    #ifdef JS_GC_DEBUG
        void* ptr = js_alloc_impl(sz, file, line);
    #else
        void* ptr = js_alloc(sz);
    #endif
    alloc_t* alloc = allocs_lookup(ptr);
    alloc->no_pointer = true;
    return ptr;    
}
Beispiel #3
0
Datei: gc.c Projekt: filp/jsos
static void js_gc_mark()
{
    uint32_t stack_dummy;
    intptr_t** ptrptr = (intptr_t**)stack_top;
    alloc_t* alloc;
    global_t* g;
    while((intptr_t)ptrptr > (intptr_t)&stack_dummy) {
        alloc = allocs_lookup(*ptrptr);
        if(alloc) {
            js_gc_mark_allocation(alloc);
        }
        ptrptr--;
    }
    for(g = globals; g; g = g->next) {
        uint32_t i;
        for(i = 0; i < g->size; i++) {
            alloc = allocs_lookup(g->ptr[i]);
            if(alloc) {
                js_gc_mark_allocation(alloc);
            }
        }
    }
}
Beispiel #4
0
Datei: gc.c Projekt: filp/jsos
static void js_gc_mark_allocation(alloc_t* alloc)
{
    intptr_t** ptrptr = alloc->ptr;
    alloc_t* suballoc;
    if(alloc->flag == current_mark_flag) {
        return;
    }
    alloc->flag = current_mark_flag;
    if(alloc->no_pointer) {
        return;
    }
    while((intptr_t)ptrptr < (intptr_t)((intptr_t)alloc->ptr + alloc->size)) {
        suballoc = allocs_lookup(*ptrptr);
        if(suballoc) {
            js_gc_mark_allocation(suballoc);
        }
        ptrptr++;
    }
}
Beispiel #5
0
Datei: gc.c Projekt: filp/jsos
void* js_realloc(void* ptr, size_t sz)
#endif
{
    #ifdef JS_GC_DEBUG
        void* new_ptr = js_alloc_impl(sz, file, line);
    #else
        void* new_ptr = js_alloc(sz);
    #endif
    alloc_t* alloc = allocs_lookup(ptr);
    if(alloc) {
        memcpy(new_ptr, ptr, sz > alloc->size ? alloc->size : sz);
    }
    return new_ptr;
    // @TODO attempt to realloc existing block
    /*
    alloc_t* alloc = allocs_lookup(ptr);
    void* new_ptr;
    uint16_t new_hash;
    if(alloc == NULL) {
        return js_alloc(sz);
    }
    new_ptr = realloc(ptr, sz);
    if(ptr == new_ptr) {
        return ptr;
    }
    alloc->ptr = new_ptr;
    alloc->size = sz;
    if(alloc->prev) {
        alloc->prev->next = alloc->next;
    }
    if(alloc->next) {
        alloc->next->prev = alloc->prev;
    }
    new_hash = pointer_hash(alloc->ptr);
    alloc->prev = NULL;
    alloc->next = allocs[new_hash];
    if(alloc->next) {
        alloc->next->prev = alloc;
    }
    allocs[new_hash] = alloc;
    return new_ptr;
    */
}
Beispiel #6
0
void* js_realloc(void* ptr, size_t sz)
#endif
{
    alloc_t* alloc = allocs_lookup(ptr);
    void* new_ptr;
    uint16_t new_hash;
    if(alloc == NULL) {
        #ifdef JS_GC_DEBUG
            return js_alloc_impl(sz, file, line);
        #else
            return js_alloc(sz);
        #endif
    }
    new_ptr = realloc(ptr, sz);
    if(new_ptr == ptr) {
        alloc->size = sz;
        return new_ptr;
    }
    
    // pointer has changed, so unlink this alloc from its bucket:    
    new_hash = pointer_hash(new_ptr);
    if(alloc->prev == NULL) {
        allocs[pointer_hash(ptr)] = alloc->next;
    } else {
        alloc->prev->next = alloc->next;
    }
    if(alloc->next) {
        alloc->next->prev = alloc->prev;
    }
    // change the pointer and size:
    alloc->ptr = new_ptr;
    alloc->size = sz;
    // add to the start of its new bucket:
    alloc->next = allocs[new_hash];
    if(alloc->next) {
        alloc->next->prev = alloc;
    }
    alloc->prev = NULL;
    allocs[new_hash] = alloc;
    // and return the pointer to the new allocation:
    return new_ptr;
}