From: John Hodge Date: Fri, 20 Jul 2012 02:36:30 +0000 (+0800) Subject: Kernel - Added pmemmap sorting and fixed memmove X-Git-Tag: rel0.15~611^2~12 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=31037f330a0fc04936a4a1c608e7fd8d377dea89;p=tpg%2Facess2.git Kernel - Added pmemmap sorting and fixed memmove --- diff --git a/KernelLand/Kernel/libc.c b/KernelLand/Kernel/libc.c index 7a7cc056..08ebc623 100644 --- a/KernelLand/Kernel/libc.c +++ b/KernelLand/Kernel/libc.c @@ -688,7 +688,6 @@ int rand(void) void *memmove(void *__dest, const void *__src, size_t len) { - size_t block_size; char *dest = __dest; const char *src = __src; void *ret = __dest; @@ -705,6 +704,8 @@ void *memmove(void *__dest, const void *__src, size_t len) if( (tVAddr)dest < (tVAddr)src ) return memcpy(dest, src, len); + #if 0 + size_t block_size; if( (tVAddr)dest < (tVAddr)src ) block_size = (tVAddr)src - (tVAddr)dest; else @@ -721,6 +722,13 @@ void *memmove(void *__dest, const void *__src, size_t len) } memcpy(dest, src, len); return ret; + #else + for( int i = len; i--; ) + { + dest[i] = src[i]; + } + return ret; + #endif } diff --git a/KernelLand/Kernel/pmemmap.c b/KernelLand/Kernel/pmemmap.c index 50f8de19..21ced24d 100644 --- a/KernelLand/Kernel/pmemmap.c +++ b/KernelLand/Kernel/pmemmap.c @@ -5,6 +5,7 @@ * pmemmap.c * - Physical memory map manipulation */ +#define DEBUG 0 #include #include @@ -13,7 +14,7 @@ void PMemMap_DumpBlocks(tPMemMapEnt *map, int NEnts) { for( int i = 0; i < NEnts; i ++ ) { - Log_Debug("Arch", "%i: %i 0x%02x %08llx+%llx", + Log_Debug("PMemMap", "%i: %i 0x%02x %08llx+%llx", i, map[i].Type, map[i].NUMADomain, map[i].Start, map[i].Length ); @@ -30,11 +31,11 @@ int PMemMap_SplitBlock(tPMemMapEnt *map, int NEnts, int MaxEnts, int Block, Uint // out of space return NEnts; } - Block ++; if( Block < NEnts ) { - // Can't be anything after - memmove(&map[Block+1], &map[Block], (NEnts - Block)*sizeof(map[0])); + LOG("Moving %i entries from %i to %i", (NEnts - Block-1), Block+1, Block); + memmove(&map[Block+2], &map[Block+1], (NEnts - Block)*sizeof(map[0])); } + Block ++; NEnts ++; // New (free) block @@ -64,7 +65,8 @@ int PMemMap_CompactMap(tPMemMapEnt *map, int NEnts, int MaxEnts) // Ok, they should be together map[i-1].Length += map[i].Length; memmove(&map[i], &map[i+1], (NEnts - (i+1))*sizeof(map[0])); - + LOG("Joined %i and %i into %llx+%llx", i-1, i, map[i-1].Start, map[i-1].Length); + // Counteract the i++ in the loop iterator i --; NEnts --; @@ -85,8 +87,24 @@ int PMemMap_ValidateMap(tPMemMapEnt *map, int NEnts, int MaxEnts) } if( bNeedsSort ) { - Log_Warning("Arch", "TODO: Impliment memory map sorting"); - // TODO: Sort memory map + // Use a selection/swap sort + for( int i = 0; i < NEnts; i ++ ) + { + int sel = i; + for( int j = i+1; j < NEnts; j ++ ) + { + if( map[j].Start < map[sel].Start ) + sel = j; + } + if( sel != i ) { + LOG("Swapping %i and %i", i, sel); + LOG(" - %llx+%llx", map[i].Start, map[i].Length); + LOG(" - %llx+%llx", map[sel].Start, map[sel].Length); + tPMemMapEnt tmp = map[i]; + map[i] = map[sel]; + map[sel] = tmp; + } + } } // Ensure that the map has no overlaps @@ -101,7 +119,8 @@ int PMemMap_ValidateMap(tPMemMapEnt *map, int NEnts, int MaxEnts) ); } - return PMemMap_CompactMap(map, NEnts, MaxEnts); + NEnts = PMemMap_CompactMap(map, NEnts, MaxEnts); + return NEnts; }