int main(int argc, char *argv[]) { struct Node *head = createNode(11.0); append(head, 22.1); append(head, 33.2); append(head, 44.3); printList(head); insertIntoList(head, 2, 999.99); printList(head); printf("now deleting list\n"); deleteList(head); return 0; }
/* * coalesceFreeBlocks - add a new item to the free list and coalesce */ static LPVMEML coalesceFreeBlocks( LPVMEMHEAP pvmh, LPVMEML pnew ) { LPVMEML pvmem; LPVMEML prev; FLATPTR end; BOOL done; pvmem = pvmh->freeList; pnew->next = NULL; end = pnew->ptr + pnew->size; prev = NULL; done = FALSE; /* * try to merge the new block "pnew" */ while( pvmem != NULL ) { if( pnew->ptr == (pvmem->ptr + pvmem->size) ) { /* * new block starts where another ended */ pvmem->size += pnew->size; done = TRUE; } else if( end == pvmem->ptr ) { /* * new block ends where another starts */ pvmem->ptr = pnew->ptr; pvmem->size += pnew->size; done = TRUE; } /* * if we are joining 2 blocks, remove the merged on from the * list and return so that it can be re-tried (we don't recurse * since we could get very deep) */ if( done ) { if( prev != NULL ) { prev->next = pvmem->next; } else { pvmh->freeList = pvmem->next; } MemFree( pnew ); return pvmem; } prev = pvmem; pvmem = pvmem->next; } /* * couldn't merge, so just add to the free list */ insertIntoList( pnew, (LPLPVMEML) &pvmh->freeList ); return NULL; } /* coalesceFreeBlocks */
/* * linVidMemAlloc - alloc some flat video memory */ static FLATPTR linVidMemAlloc( LPVMEMHEAP pvmh, DWORD size ) { LPVMEML pvmem; LPVMEML prev; LPVMEML pnew_free; DWORD new_size; if( size == 0 || pvmh == NULL ) { return (FLATPTR) NULL; } size = (size+(BLOCK_BOUNDARY-1)) & ~(BLOCK_BOUNDARY-1); /* * run through free list, looking for the closest matching block */ prev = NULL; pvmem = pvmh->freeList; while( pvmem != NULL ) { if( pvmem->size >= size ) { /* * compute new free list item; only make a new free item if * it will be bigger than MIN_SPLIT_SIZE */ new_size = pvmem->size - size; if( new_size > MIN_SPLIT_SIZE ) { pnew_free = MemAlloc( sizeof( VMEML ) ); if( pnew_free == NULL ) { return (FLATPTR) NULL; } pnew_free->size = new_size; pnew_free->ptr = pvmem->ptr + size; } else { pnew_free = NULL; size += new_size; } /* * remove the old free item from the free list, add new one */ if( prev != NULL ) { prev->next = pvmem->next; } else { pvmh->freeList = pvmem->next; } if( pnew_free != NULL ) { insertIntoList( pnew_free, (LPLPVMEML) &pvmh->freeList ); } /* * insert allocated item into allocation list */ pvmem->size = size; insertIntoList( pvmem, (LPLPVMEML) &pvmh->allocList ); return pvmem->ptr; } prev = pvmem; pvmem = pvmem->next; } return (FLATPTR) NULL; } /* linVidMemAlloc */
void VoronoiSeedsGenerator::generateSeeds( std::vector<Seed>& listOfSeeds ) { /* * In order to retain the current number of seeds by subdivision of the * grid, we use a bi-dimensional array. */ int** nbSeedsBySub = new int* [m_nbOfHeightSubdivisions]; for (int i = 0; i < m_nbOfHeightSubdivisions; i++) { nbSeedsBySub[i] = new int[m_nbOfWidthSubdivisions]; for (int j = 0; j < m_nbOfWidthSubdivisions; j++) { nbSeedsBySub[i][j] = 0; } } /* * So as to retain the repartition of the inserted seeds, we need to allocate another array. * This one is dynamically allocated since it will be necessary to use it as an argument * for a function. */ std::vector<Seed>** seedsBySub = new std::vector<Seed>* [m_nbOfHeightSubdivisions]; for (int i = 0; i < m_nbOfHeightSubdivisions; i++) { seedsBySub[i] = new std::vector<Seed> [m_nbOfWidthSubdivisions]; } /* * Initializing the random generator which is going to be used in order to * generate the seeds. */ std::random_device randomDevice; std::mt19937 generator(randomDevice()); std::normal_distribution<float> widthDistrib(m_width/2.0, m_width/5.0); std::normal_distribution<float> heightDistrib(m_height/2.0, m_height/5.0); /* * Main loop of the function in which the seeds are generated. * On top of that, in order to ease the implementation of the "Whittaker * step", the seeds have to be sorted according to their distance to the * center of the map. * So as to do so, the seeds are sorted by "insertion" in a temporary list, * and then pushed back in the given vector. */ std::list<Seed> tmpList; int currentNbOfSeeds = 0; while (currentNbOfSeeds < m_nbOfSeeds) { float wPosition = widthDistrib(generator); float hPosition = heightDistrib(generator); /* * Testing if the subdivision which is going to contain the new seed * is "full". */ int widthID = (int)(floor(wPosition/m_width*m_nbOfWidthSubdivisions)); int heightID = (int)(floor(hPosition/m_height*m_nbOfHeightSubdivisions)); if ( (widthID >= 0) && (widthID < m_nbOfWidthSubdivisions) && (heightID >=0) && (heightID < m_nbOfHeightSubdivisions) ) { if (nbSeedsBySub[heightID][widthID] < m_maxNbOfSeedsBySubdivision) { Seed seed(wPosition, hPosition); /* * Inserting only if the new seed is at a minimal distance * of the other ones previously inserted. */ if (isMinDistVerified(seedsBySub, widthID, heightID, seed)) { insertIntoList(tmpList, seed); currentNbOfSeeds++; nbSeedsBySub[heightID][widthID]++; seedsBySub[heightID][widthID].push_back(seed); } } } } /* * Freeing the allocated memory zone related to seeds' repartition. */ for (int i = 0; i < m_nbOfHeightSubdivisions; i++) { seedsBySub[i]->clear(); delete [] seedsBySub[i]; delete[] nbSeedsBySub[i]; } delete [] seedsBySub; delete[] nbSeedsBySub; /* * Finally, we just have to insert the content of the list into the * given vector. */ for ( auto iterator = tmpList.begin(); iterator != tmpList.end(); iterator++ ) { listOfSeeds.push_back(*iterator); } }