void NNLayer::alloc_space () { assert(n_neuron > 0 && n_input > 0); weight = rm_alloc(n_neuron, n_input+1); wbackup = rm_alloc(n_neuron, n_input+1); wdelta = rm_alloc(n_neuron, n_input+1); wdel_old = rm_alloc(n_neuron, n_input+1); wdir = rm_alloc(n_neuron, n_input+1); sig_der = rv_alloc(n_neuron); for (size_t i = 0; i < n_neuron; i++) rv_clear(wdelta[i], n_input+1); }
/** * Allocate a block of memory * * @param sz size of the required block * @returns pointer to block */ void *kmalloc(u32 sz) { kerror(ERR_DETAIL, "Allocating %d bytes of memory", sz); // We don't want two processes using the same memory block! lock(&alloc_lock); // Find the smallest memory block that we can use u32 idx = find_hole(sz); // Couldn't find one... if(idx == 0xFFFFFFFF) return 0; int block = idx >> 16; int index = idx & 0xFFFF; if(empty_slots(block) == 4) // Get ready ahead of time { u32 asz = ALLOC_BLOCK * sizeof(struct alcent); u32 idx = find_hole(asz); if(idx == 0xFFFFFFFF) kpanic("Could not create another allocation block!"); int block = idx >> 16; int index = idx & 0xFFFF; if(allocs[block][index].size == asz) { allocs[block][index].used = 1; allocs[get_free_block()] = (struct alcent *)allocs[block][index].addr; } else { allocs[block][index].size -= asz; struct alcent ae = { .valid = 1, .used = 1, .addr = allocs[block][index].addr, .size = asz }; allocs[block][index].addr += asz; add_alloc(&ae); allocs[get_free_block()] = (struct alcent *)ae.addr; } } // If the previous block of code was used, we may have to reinitialize these idx = find_hole(sz); if(idx == 0xFFFFFFFF) return 0; block = idx >> 16; index = idx & 0xFFFF; if(allocs[block][index].size == sz) { allocs[block][index].used = 1; unlock(&alloc_lock); kerror(ERR_DETAIL, " -> %08X W", allocs[block][index].addr); return (void *)allocs[block][index].addr; } allocs[block][index].size -= sz; // We are using part of this block struct alcent ae = { .valid = 1, .used = 1, .addr = allocs[block][index].addr, .size = sz }; allocs[block][index].addr += sz; // We don't want anything else using the allocated memory add_alloc(&ae); // We will just assume this worked, the worst that could happen is we can't `free` it (FIXME) // Let other processes allocate memory unlock(&alloc_lock); kerror(ERR_DETAIL, " -> %08X P", ae.addr); return (void *)ae.addr; } /** * Free an allocated block of memory * * @param ptr pointer to the previously allocated memory block */ void kfree(void *ptr) { kerror(ERR_DETAIL, "Freeing %08X", ptr); lock(&alloc_lock); int i, j = 0; // Find the corresponding memory block for(; j < ALLOC_BLOCKS; j++) { if(!allocs[j]) continue; for(i = 0; i < ALLOC_BLOCK; i++) if(allocs[j][i].valid) // Is it valid? if(allocs[j][i].addr == (u32)ptr) // Is it the correct block? rm_alloc(j, i); // Free it! } unlock(&alloc_lock); }