int insert(int offset, long long key) { long long key_promoted = -1; int return_value = -1; int new_offset = -1; int offset_child_right = -1; int offset_child_left = -1; page_tree *page; // verifica se eh necessario criar um novo no raiz if ((return_value = insertKey(offset, key, &offset_child_left, &offset_child_right, &key_promoted)) == PROMOTED) { // aloca nova pagina na ram allocPage(&page); // insere chave na nova pagina criada (ainda na RAM) pageInsert(page, key_promoted, offset_child_left, offset_child_right); //emanuel page->child[0] = offset_left; page->child[0] = offset_child_left; // move o ponteiro para o final do arquivo para pegar a posicao da nova pagina fseek(bTreeFile, 0L, SEEK_END); new_offset = ftell(bTreeFile); // salva a pagina no disco savePage(new_offset, page); update_root_offset(new_offset); free(page); } return return_value; }
/** * Allocates a certain amount of memory. The pointer which is * returned is only valid until reset() is called. * @param size Size of the memory allocated (in bytes). * @return Pointer to the allocated memory. */ void *allocate(unsigned int size) { // TODO: Handle large allocations properly? tbb::spin_mutex::scoped_lock lock(mutex); if (used + size <= pagesize) { // We have enough memory on this page void *memory = (char*)currentmemory + used; used += size; return memory; } else if (freememory.empty()) { // Allocate a new page usedmemory.push_back(currentmemory); currentmemory = allocPage(pagesize); used = size; return currentmemory; } else { // Start a new page usedmemory.push_back(currentmemory); currentmemory = freememory.back(); freememory.pop_back(); used = size; return currentmemory; } }
void main() { int foo = add(3,4); char* ptr = (char*)0x900000; allocPage((void*)ptr); int size = 5000; char* a_ptr = (char*)malloc(sizeof(char) * size); if (a_ptr == NULL) { return; } int i; for (i=0; i<4096; i++) { *ptr = 10; ptr++; } char* subptr = a_ptr; for (i=0; i<size; i++) { *subptr = 10; subptr++; } a_ptr = (char*)malloc(sizeof(char) * 20); for (i=0; i<20; i++) { *a_ptr = 10; a_ptr++; } }
void *MemoryPool::allocChunk() { if (!_next) // No free chunks left? Allocate a new page allocPage(); assert(_next); void *result = _next; _next = *(void**)result; return result; }
/** * Constructor. * @param pagesize Size of one page allocated by the class. Should * be much larger than the objects which are going to be allocated. */ MemoryPool(unsigned int pagesize = 1048576) : used(0) { // Round up page size to 4k pages pagesize = (pagesize + 0xFFF) & ~0xFFF; this->pagesize = pagesize; // Allocate a single page currentmemory = allocPage(pagesize); }
/** * Create a new stream between processes: * 1) Create a ring buffer to hold the data * 2) Map the buffer to read_vaddr and write_vaddr in the respective process * memory spaces * 3) Initialize the stream pointers residing in shared memory */ TKStreamID streamCreate( unsigned int size, TKVProcID read_owner, TKStreamPointer *read_ptr, TKVProcID write_owner, TKStreamPointer *write_ptr) { TKStreamInfo *info; // Round up to the nearest 4k bytes int num_pages = (size + 0xfff) >> 12; void *read_vaddr, *write_vaddr; void *first_buffer; int i; info = &tk_stream_table[tk_num_streams++]; info->stream_id = tk_num_streams; info->read_owner = read_owner; info->read_ptr = read_ptr; info->write_owner = write_owner; info->write_ptr = write_ptr; info->size = size; info->num_pages = num_pages; // Map the pages into the user memory spaces for (i = 0; i < info->num_pages; i++) { int j; void *addr; void *buffer = allocPage(); if (i == 0) { first_buffer = buffer; } // Fill with empty identifier for (j = 0; j < 1024; j++) { ((unsigned int *)buffer)[j] = TKS_ID_EMPTY; } addr = procMapHeapPage(read_owner, (unsigned int)buffer); if (i == 0) { read_vaddr = addr; } addr = procMapHeapPage(write_owner, (unsigned int)buffer); if (i == 0) { write_vaddr = addr; } } // Map the first page again after the last page, so writes off the end can // magically just wrap around procMapHeapPage(read_owner, (unsigned int)first_buffer); procMapHeapPage(write_owner, (unsigned int)first_buffer); // Initialize the stream pointers streamInitStreams(read_ptr, write_ptr, read_vaddr, write_vaddr, size); kprintf("Created stream %d, size %X, %d pages\n", info->stream_id, info->size, info->num_pages); kprintf(" -> Mapped reader %d (%X) to address %X\n", read_owner, read_ptr, read_vaddr); kprintf(" -> Mapped writer %d (%X) to address %X\n", write_owner, write_ptr, write_vaddr); return info->stream_id; }
CoorPanel *createCoorPanel(int x, int y, int w, int h) { CoorPanel *coorPanel = (CoorPanel *)allocPage(sizeof(CoorPanel)); coorPanel = (CoorPanel *)initWithViewFunction((View*)coorPanel, x, y, w, h); (*coorPanel).canvas = createView(x, y, w, h); (*coorPanel).initPanel = initCoorPanel; (*coorPanel).initPanel(coorPanel); (*coorPanel).view.onMouseDown = onMouseDown; return coorPanel; }
/** * Frees the memory returned by all previous calls to allocate() and * reallocates all memory pages with a new page size. * @param size Amount of memory initially allocated. * @param pagesize Size of one memory page. */ void reset(unsigned int size, unsigned int pagesize) { // Round up page size to 4k pages pagesize = (pagesize + 0xFFF) & ~0xFFF; // Free all existing pages freePage(currentmemory, this->pagesize); for (unsigned int i = 0; i < usedmemory.size(); i++) freePage(usedmemory[i], this->pagesize); for (unsigned int i = 0; i < freememory.size(); i++) freePage(freememory[i], this->pagesize); usedmemory.clear(); // Set new page size this->pagesize = pagesize; // Allocate new pages unsigned int pagecount = (size + pagesize - 1) / pagesize; currentmemory = allocPage(pagesize); freememory.resize(pagecount - 1); for (unsigned int i = 0; i < pagecount - 1; i++) freememory[i] = allocPage(pagesize); // Reset current memory page used = 0; }
void Video::init() { _newPal = 0xFF; for (int i = 0; i < 4; ++i) { _pagePtrs[i] = allocPage(); } _curPagePtr3 = getPagePtr(1); _curPagePtr2 = getPagePtr(2); changePagePtr1(0xFE); _interpTable[0] = 0x4000; for (int i = 1; i < 0x400; ++i) { _interpTable[i] = 0x4000 / i; } }
void* alloc(size_t size, bool exact=false) { assert(size<=80 || exact); /// Align to word boundary size += ((8 - (size & 7)) & 7); HeapPage* p = _page; if (exact || _page==NULL || _page->used+size >= _page->size) p = allocPage(size,exact); char* ret = p->data+p->used; p->used += size; _free_mem -= size; assert(_alloced_mem >= _free_mem); return ret; }
kma_page_t* get_page() { static int id = 0; kma_page_t* res; kma_page_stats.num_requested++; kma_page_stats.num_in_use++; res = (kma_page_t*) malloc(sizeof(kma_page_t)); res->id = id++; res->size = kma_page_stats.page_size; res->ptr = allocPage(); assert(res->ptr != NULL); return res; }
/** 存放一个事务记录,如果空间不够,则分配新的页 * @param rec 需要存在的事务记录 */ TxnRec* TxnRecManager::push(Session *session, TxnRec *rec) { TxnRec *ret = NULL; TxnRecPage *page = m_curPage; //脏读 latchPage(session, page, Exclusived); //如果页面不能再容纳别的记录 while (page->m_recCnt == m_recPerPage) { unLatchPage(session, page, Exclusived); m_lock.lock(Exclusived, __FILE__, __LINE__); //重判断,因为此时页面分配可能已经完成了 if (page == m_curPage) { if (m_curPage == m_tail) { while(!allocPage(session, 1)) { //TODO 在页面不够的情况下需要触发整理内存线程 } } m_curPage = m_curPage->m_next; assert(m_curPage != NULL); } page = m_curPage; m_lock.unlock(Exclusived); latchPage(session, page, Exclusived); } ret = (TxnRec *)((byte *)page + sizeof(TxnRecPage) + m_curPage->m_recCnt*sizeof(TxnRec)); memcpy(ret, rec, sizeof(TxnRec)); page->m_recCnt++; if (rec->m_txnId > page->m_maxTxnId) { page->m_maxTxnId = rec->m_txnId; } if (rec->m_txnId < page->m_minTxnId) { page->m_minTxnId = rec->m_txnId; } unLatchPage(session, page, Exclusived); return ret; }
/** * Frees the memory returned by all previous calls to allocate(). * Different to reset(), this function allocates as many pages as * are needed to allocate size bytes, reusing existing pages if * possible. * @param size Amount of memory initially allocated. */ void reset(unsigned int size) { unsigned int pagecount = (size + pagesize - 1) / pagesize; // We have one page in currentmemory pagecount -= 1; // Allocate enough pages, reusing existing pages if (freememory.size() >= pagecount) { for (unsigned int i = 0; i < usedmemory.size(); i++) freePage(usedmemory[i], pagesize); usedmemory.clear(); for (unsigned int i = pagecount; i < freememory.size(); i++) freePage(freememory[i], pagesize); freememory.resize(pagecount); } else if (freememory.size() + usedmemory.size() >= pagecount) { for (unsigned int i = 0; i < pagecount - freememory.size(); i++) freememory.push_back(usedmemory[i]); for (unsigned int i = pagecount - freememory.size(); i < usedmemory.size(); i++) freePage(usedmemory[i], pagesize); usedmemory.clear(); } else { unsigned int freepages = freememory.size(); freememory.resize(pagecount); for (unsigned int i = 0; i < usedmemory.size(); i++) freememory[freepages + i] = usedmemory[i]; freepages += usedmemory.size(); usedmemory.clear(); for (unsigned int i = freepages; i < pagecount; i++) freememory[i] = allocPage(pagesize); } // Reset current memory page used = 0; }
View *initWithViewFunction(View *view, int x, int y, int width, int height) { (*view).buffer = (u8 *)allocPage(width*height*SCREEN_DENSITY); (*view).parentView = null; (*view).reference = 1; (*view).subViewNum = 0; (*view).visible = TRUE; (*view).width = width; (*view).height = height; (*view).x = x; (*view).y = y; (*view).clearView = clearView; (*view).addSubView = addSubView; (*view).removeSubView = removeSubView; (*view).setBackgroundColor = setBackgroundColor; (*view).processMouseDownEvent = processMouseDownEvent; (*view).refreshRectView = refreshRectView; (*view).deleteView = deleteView; (*view).releaseView = releaseView; (*view).retainView = retainView; return view; }
void streamInit() { // Create a table of streams tk_stream_table = (TKStreamInfo *)allocPage(); tk_num_streams = 0; }
//split(key_promotion, offset_child_promoted, curr_page, key_promoted, child_right_promoted, &new_page); void split(const long long key_input, const int child_right_input, page_tree* page, long long* key_promoted, int* child_left_promoted, int* child_right_promoted, page_tree** new_page) { int i; int size_split; int key_idx, child_idx; //printf("inserindo chave %lld\n", key_input); /*aloca nova pagina *note que new_page *é ponteiro ** */ /*size_split terá o numero de chaves * que iremos colocar na pagina nova */ allocPage(new_page); size_split = (ORDER -1)/2; /*copia as chaves do split para * a nova pagina */ for(i = 0, key_idx = ORDER - 2, child_idx = ORDER -1; i < size_split; i++, key_idx--, child_idx --) { pageInsert(*new_page, page->keys[key_idx], page->child[child_idx-1], page->child[child_idx]); page->child[child_idx] = NIL; //page->child[child_idx-1] = NIL; page->nKeys--; } /*verifica se a chave atual é maior que a chave da direita * da página antiga. CAso seja maior, será inserido * na nova página, caso contrário, será inserido nova página */ // printf("comparando %d com %d\n", key_input, page->keys[ORDER-2 - size_split]); if(key_input > page->keys[ORDER-2 - size_split]) { /*insere a chave atual*/ pageInsert(*new_page, key_input, NIL, child_right_input); } else{ /*antes o ultimo nó da pagina antiga é transferido * pra pagina nova. E depois inseriamos a chave atual na pagina antiga */ pageInsert(*new_page, page->keys[ORDER -2 -size_split], page->child[ORDER-1-size_split-1], page->child[ORDER-1-size_split]); page->child[ORDER-1-size_split] = NIL; page->nKeys--; //page->nKeys--; pageInsert(page, key_input, NIL, child_right_input); } /*promovemos a chave da esquerda da nova pagina * pois será a menor */ *key_promoted = (*new_page)->keys[0]; *child_left_promoted = (*new_page)->child[0]; //printf("==>child left promoted vale %d e key promovida vale %d\n", *child_left_promoted, *key_promoted); /*deslocamos as chaves de nova * pagina para esquerda */ for(i = 0; i < (*new_page)->nKeys-1; i++) { (*new_page)->keys[i] = (*new_page)->keys[i+1]; (*new_page)->child[i] = (*new_page)->child[i+1]; } (*new_page)->child[i] = (*new_page)->child[i+1]; (*new_page)->child[i+1] = NIL; /*remove de fato a chave na nova pagina*/ (*new_page)->nKeys--; /*obtem o offset_left: Este offset é o offset * da pagina antiga (que foi particionada) * */ offset_left = searchKeyOnBTree(offset_root, page->keys[0]); *child_left_promoted = offset_left; //printf("||SPLIT e child left vale %d, procurei pela chave %d\n", offset_left, page->keys[0]); /*obtem o offset da nova pagina: * basta posicionar no fim do arquivo e retornar o valor * do offset em bytes */ fseek(bTreeFile, 0L, SEEK_END); *child_right_promoted = ftell(bTreeFile); //printf("conteudo da pagina atual:\n"); //printf("%d\n", (*new_page)->keys[0]); }
void TxnRecManager::init(Session *session) { allocPage(session, FIRST_ALLOC_PAGE_SIZE); m_curPage = m_head; }