Advertisement
Guest User

Untitled

a guest
Sep 9th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. void * operator new(std::size_t size) {
  2. constexpr std::size_t align = 4096;
  3. std::size_t request = sizeof(Plate<rows, columns>) + align;
  4. const std::size_t needed = request + sizeof(void*);
  5. void * buffer = ::operator new(needed);
  6. void * starting_location = buffer + sizeof(void*);
  7. void * ptr = nonstd::align(align, sizeof(Plate<rows,columns>), starting_location, request);
  8.  
  9. ((void **) ptr)[-1] = buffer;
  10. return ptr;
  11. }
  12.  
  13.  
  14. namespace nonstd {
  15.  
  16. /* At time of this writing I did not have an implementation of C++11's
  17. * std::align; this is modified code from:
  18. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57350
  19. */
  20. inline void *align(std::size_t alignment, std::size_t size, void *&ptr, std::size_t &space ) {
  21. std::uintptr_t pn = reinterpret_cast<std::uintptr_t>( ptr );
  22. std::uintptr_t aligned = (pn + alignment - 1) & - alignment;
  23. std::size_t padding = aligned - pn;
  24. if (space < size + padding) {
  25. return nullptr;
  26. }
  27. space -= padding;
  28. return ptr = reinterpret_cast<void *>(aligned);
  29. }
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement