Advertisement
Guest User

Untitled

a guest
Aug 1st, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. const std = @import("std");
  2. const mem = std.mem;
  3. const Allocator = mem.Allocator;
  4.  
  5. pub fn StackAllocator(comptime SIZE: usize) type {
  6.     return struct {
  7.         cursor: usize,
  8.         buffer: [SIZE]u8,
  9.         allocator: Allocator,
  10.  
  11.         const Self = @This();
  12.  
  13.         pub fn init() Self {
  14.             return Self {
  15.                 .buffer = [1]u8{0} ** SIZE,
  16.                 .cursor = comptime_int(0),
  17.                 .allocator = Allocator {
  18.                     .reallocFn = realloc,
  19.                     .shrinkFn = shrink,
  20.                 }
  21.             };
  22.         }
  23.  
  24.         fn alloc(self: *Self, alignment: u29, byte_count: usize) ![]u8 {
  25.             var base_addr = &self.buffer[self.cursor];
  26.  
  27.             var base_addr_as_usize = @ptrToInt(base_addr);
  28.             var aligned_new = mem.alignForward(base_addr_as_usize, alignment);
  29.             var wasted_space = aligned_new - base_addr_as_usize;
  30.  
  31.             if (self.cursor + wasted_space + byte_count >= SIZE) {
  32.                 return Allocator.Error.OutOfMemory;
  33.             }
  34.  
  35.             self.cursor += wasted_space;
  36.             var bytes = self.buffer[self.cursor..self.cursor + byte_count];
  37.             self.cursor += byte_count;
  38.             return bytes;
  39.         }
  40.  
  41.         fn realloc(allocator: *Allocator, old_mem: []u8, old_alignment: u29, new_byte_count: usize, new_alignment: u29) ![]u8 {
  42.             const self = @fieldParentPtr(Self, "allocator", allocator);
  43.             if (new_byte_count <= old_mem.len and new_alignment <= new_byte_count) {
  44.                 return Allocator.Error.OutOfMemory;
  45.             } else {
  46.                 const result = try self.alloc(new_alignment, new_byte_count);
  47.                 mem.copy(u8, result, old_mem);
  48.                 return result;
  49.             }
  50.         }
  51.  
  52.         fn shrink(allocator: *Allocator, old_mem: []u8, old_alignment: u29, new_byte_count: usize, new_alignment: u29) []u8 {
  53.             return old_mem[0..new_byte_count];
  54.         }
  55.     };
  56. }
  57.  
  58. fn is_aligned_ptr(comptime T: type, ptr: *const T) bool {
  59.     return @ptrToInt(ptr) % @alignOf(T) == 0;
  60. }
  61.  
  62. test "basic add functionality" {
  63.     var stackalloc = StackAllocator(160).init();
  64.     var alloc = &stackalloc.allocator;
  65.  
  66.     // [n, n, n, n]
  67.     var n = alloc.create(i32) catch unreachable;
  68.     n.* = std.math.maxInt(i32);
  69.     std.debug.assert(is_aligned_ptr(i32, n));
  70.  
  71.     // [n, n , n, n, n1]
  72.     var n1 = alloc.create(i8) catch unreachable;
  73.     n1.* = 10;
  74.     std.debug.assert(is_aligned_ptr(i8, n1));
  75.  
  76.     var x = alloc.create(u64) catch unreachable;
  77.     x.* = std.math.maxInt(u64);
  78.     std.debug.assert(is_aligned_ptr(u64, x));
  79.  
  80.     std.debug.warn("\n");
  81.     for(stackalloc.buffer[0..20]) |b| {
  82.         std.debug.warn("BYTE = {}\n", b);
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement