diff --git a/alloc.d b/alloc.d index 2448341..bcb0775 100644 --- a/alloc.d +++ b/alloc.d @@ -48,7 +48,7 @@ MAlloc(T)() } T[] -MAllocArray(T)(u64 count) +MAlloc(T)(u64 count) { void* mem = MemAlloc(T.sizeof * count); return (cast(T*)mem)[0 .. count]; @@ -61,7 +61,7 @@ MFree(T)(T* ptr) } void -MFreeArray(T)(T[] slice) +MFree(T)(T[] slice) { MemFree(slice.ptr, cast(u64)slice.length * T.sizeof); } @@ -83,7 +83,7 @@ Alloc(T)(u64 count) } T[] -AllocCopy(T)(T[] target) +Alloc(T)(T[] target) { T[] arr = Alloc!(T)(target.length); arr[] = target[]; @@ -91,7 +91,7 @@ AllocCopy(T)(T[] target) } T[] -AllocCopySlice(T)(T[] target, u64 start, u64 len) +Alloc(T)(T[] target, u64 start, u64 len) { T[] arr = Alloc!(T)(len); arr[0 .. $] = target[start .. start+len]; @@ -99,18 +99,32 @@ AllocCopySlice(T)(T[] target, u64 start, u64 len) } T[] -AllocArray(T)(u64 count) +Alloc(T)(u64 count, T set) { - return Alloc!(T)(count); + T[] arr = Alloc!(T)(count); + arr[] = set; + return arr; } T[] -ReallocArray(T)(T[] arr, u64 count) +Realloc(T)(T[] arr, u64 count) { void* mem = pureRealloc(arr.ptr, T.sizeof * count); return (cast(T*)mem)[0 .. count]; } +void +Free(T)(T[] arr) +{ + pureFree(arr.ptr); +} + +void +Free(T)(T* ptr) +{ + pureFree(ptr); +} + Arena CreateArena(u64 size) { @@ -165,9 +179,15 @@ End(TempArena* t) } T[] -AllocCopy(T)(TempArena* t, T[] target) +Alloc(T)(TempArena* t, T[] target) { - return AllocCopy!(T)(t.arena, target); + return Alloc!(T)(t.arena, target); +} + +T[] +Alloc(T)(TempArena* t, u64 count, T set) +{ + return Alloc!(T)(t.arena, count, set); } T[] @@ -177,9 +197,15 @@ Alloc(T)(TempArena* t, u64 count) } T[] -AllocArray(T)(TempArena* t, u64 count) +Alloc(T)(TempArena* t, T[] target) { - return Alloc!(T)(t.arena, count); + return Alloc!(T)(t.arena, target); +} + +T[] +Alloc(T)(TempArena* t, T[] target, u64 start, u64 len) +{ + return Alloc!(T)(t.arena, target, start, len); } T* @@ -213,13 +239,7 @@ Alloc(T)(Arena* arena, u64 count) } T[] -AllocArray(T)(Arena* arena, u64 count) -{ - return Alloc!(T)(arena, count); -} - -T[] -AllocCopy(T)(Arena* arena, T[] target) +Alloc(T)(Arena* arena, T[] target) { T[] arr = Alloc!(T)(arena, target.length); arr[] = target[]; @@ -227,13 +247,21 @@ AllocCopy(T)(Arena* arena, T[] target) } T[] -AllocCopySlice(T)(Arena* arena, T[] target, u64 start, u64 len) +Alloc(T)(Arena* arena, T[] target, u64 start, u64 len) { T[] arr = Alloc!(T)(arena, len); arr[0 .. $] = target[start .. start+len]; return arr; } +T[] +Alloc(T)(Arena* arena, u64 count, T set) +{ + T[] arr = Alloc!(T)(arena, count); + arr[] = set; + return arr; +} + T* Alloc(T)(Arena* arena) { @@ -302,18 +330,6 @@ Free(Arena* arena) } } -void -FreeArray(T)(T[] arr) -{ - pureFree(arr.ptr); -} - -void -Free(T)(T* ptr) -{ - pureFree(ptr); -} - void ResetScratch(u64 size) { @@ -335,11 +351,11 @@ ScratchAlloc(T)() T[] ScratchAlloc(T)(u64 count) { - return AllocArray!(T)(&g_scratch.arena, count); + return Alloc!(T)(&g_scratch.arena, count); } T[] -ScratchAllocCopy(T)(T[] target) +ScratchAlloc(T)(T[] target) { T[] arr = ScratchAlloc!(T)(target.length); arr[] = target[]; @@ -347,19 +363,27 @@ ScratchAllocCopy(T)(T[] target) } T[] -ScratchAllocCopySlice(T)(T[] target, u64 start, u64 len) +ScratchAlloc(T)(T[] target, u64 start, u64 len) { T[] arr = ScratchAlloc!(T)(len); arr[0 .. $] = target[start .. start+len]; return arr; } +T[] +ScratchAlloc(T)(u64 count, T set) +{ + T[] arr = ScratchAlloc!(T)(count); + arr[] = set; + return arr; +} + unittest { { u64[5] arr = [1, 2, 3, 4, 5]; - u64[] copy = AllocCopy!(u64)(arr); + u64[] copy = Alloc!(u64)(arr); assert(arr == copy); } diff --git a/assets.d b/assets.d index d179e4e..cd9b0a4 100644 --- a/assets.d +++ b/assets.d @@ -168,7 +168,7 @@ LoadAssetData(Arena* arena, string name) assert(false, "Unable to open file"); } - u8[] mem = AllocArray!(u8)(arena, f.size()); + u8[] mem = Alloc!(u8)(arena, f.size()); return f.rawRead(mem); } @@ -201,8 +201,8 @@ OpenAssetPack() Asset_Header = header_arr[0]; - Asset_Info = AllocArray!(AssetInfo)(Asset_Header.asset_count); - Asset_Data = AllocArray!(u8[])(Asset_Header.asset_count); + Asset_Info = Alloc!(AssetInfo)(Asset_Header.asset_count); + Asset_Data = Alloc!(u8[])(Asset_Header.asset_count); assert(Asset_Header.file_version == FILE_VERSION, "OpenAssetPack failure: file version incorrect"); @@ -255,7 +255,7 @@ LoadAssetData(Arena* arena, string name) { if(info.hash == hash) { - data = AllocArray!(u8)(arena, info.length); + data = Alloc!(u8)(arena, info.length); Asset_File.seek(info.offset); Asset_File.rawRead(data); assert(data != null && data.length == info.length, "LoadAssetData failure: Asset data loaded incorrectly"); @@ -281,7 +281,7 @@ LoadAssetData(string name) { if(Asset_Data[i].ptr == null) { - Asset_Data[i] = AllocArray!(u8)(info.length); + Asset_Data[i] = Alloc!(u8)(info.length); Asset_File.seek(info.offset); Asset_File.rawRead(Asset_Data[i]); assert(Asset_Data[i] != null && Asset_Data[i].length == info.length, "LoadAssetData failure: Asset data loaded incorrectly."); @@ -306,7 +306,7 @@ UnloadAssetData(string name) { if(Asset_Data[i] != null) { - FreeArray(Asset_Data[i]); + Free(Asset_Data[i]); break; } } diff --git a/fonts.d b/fonts.d index 51c0630..93bd39d 100644 --- a/fonts.d +++ b/fonts.d @@ -107,7 +107,7 @@ CreateAtlas(Arena* arena, FontFace font, f32 size, u32 dimension) assert(dimension >= 128, "Dimension must be at least 128"); FontAtlasBuf atlas = { - data: AllocArray!(u8)(arena, dimension * dimension * 4), + data: Alloc!(u8)(arena, dimension * dimension * 4), atlas: { size: size, width: dimension, diff --git a/platform.d b/platform.d index 59b6118..fc12963 100644 --- a/platform.d +++ b/platform.d @@ -896,7 +896,7 @@ ClipboardText(PlatformWindow* w, ClipboardMode mode) if(owner != null && owner.owner != 0) { - FreeArray(sel.data); + Free(sel.data); sel.data = []; Unlock(&w.cb_mut); @@ -935,7 +935,7 @@ SetClipboard(PlatformWindow* w, u8[] data, ClipboardMode mode) Selection* sel = &w.selections[mode]; if(sel.data.length > 0) { - FreeArray(sel.data); + Free(sel.data); sel.data = []; } @@ -973,7 +973,7 @@ GetClipboardSelection(PlatformWindow* w, Selection* sel) if(sel.data.length > 0 && sel.target == w.atoms[Atoms.Utf8String]) { - buf = ScratchAllocCopy(sel.data); + buf = ScratchAlloc(sel.data); } return buf; @@ -1056,7 +1056,7 @@ RetrieveSelection(PlatformWindow* w, xcb_selection_notify_event_t* ev) if(sel != null && sel.target == actual_type) { - FreeArray(sel.data); + Free(sel.data); sel.data = buf; buf = []; } @@ -1067,7 +1067,7 @@ RetrieveSelection(PlatformWindow* w, xcb_selection_notify_event_t* ev) } else { - FreeArray(buf); + Free(buf); } } @@ -1083,7 +1083,7 @@ ClearSelection(PlatformWindow* w, xcb_selection_clear_event_t* ev) { Lock(&w.cb_mut); - FreeArray(sel.data); + Free(sel.data); sel.data = []; sel.owned = false; sel.target = XCB_NONE; @@ -1534,7 +1534,7 @@ WatchDirectory(string dir, WatchType type, bool blocking = false) Watcher watcher = { arena: CreateArena(MB(4)), - buffer: AllocArray!(u8)(MB(1)), + buffer: Alloc!(u8)(MB(1)), blocking: blocking, watched_dir: (cast(u8*)dir.ptr)[0 .. dir.length], }; @@ -1585,9 +1585,9 @@ ViewChanges(Watcher* watcher) i64 from; } - Moved[] moved = AllocArray!(Moved)(&watcher.arena, (count/2)+1); + Moved[] moved = Alloc!(Moved)(&watcher.arena, (count/2)+1); i64 m_count = 0; - events = AllocArray!(WatchEvent)(&watcher.arena, count); + events = Alloc!(WatchEvent)(&watcher.arena, count); count = 0; i = 0; while (i < length) diff --git a/util.d b/util.d index d6608eb..96d21f7 100644 --- a/util.d +++ b/util.d @@ -481,7 +481,7 @@ CreateHashTable(K, V)(u64 size) { Arena arena = CreateArena(MB(4)); auto nil = Alloc!(Node!(KVPair!(K, V)))(&arena); - auto lists = AllocArray!(SLList!(KVPair!(K, V)))(&arena, size); + auto lists = Alloc!(SLList!(KVPair!(K, V)))(&arena, size); HashTable!(K, V) table = { arena: arena, @@ -570,7 +570,7 @@ GetList(K, V)(HashTable!(K, V)* ht, K key) KVPair!(K, V)*[] GetAllNodes(K, V)(Arena* arena, HashTable!(K, V)* ht) { - KVPair!(K, V)*[] pairs = AllocArray!(KVPair!(K, V)*)(arena, ht.node_count); + KVPair!(K, V)*[] pairs = Alloc!(KVPair!(K, V)*)(arena, ht.node_count); if(ht.node_count > 0) {