Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: theagentd (posted 2016-03-11 11:43:46, viewed 500 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139  
package test;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;

import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.system.Pointer;

public class BufferStack {

   private static final int ALIGNMENT = 4096; //page size alignment >___> Not sure if it has any benefit...
   
   private static final int DEFAULT_DATA_CAPACITY = 128*1024;
   private static final int DEFAULT_PUSH_CAPACITY = 128;
   
   
   private long address;
   
   private long[] savedAddresses;
   private int position;
   
   public BufferStack() {
      this(DEFAULT_DATA_CAPACITY, DEFAULT_PUSH_CAPACITY);
   }
   
   public BufferStack(int dataCapacity, int stackCapacity) {
      
      address = MemoryUtil.nmemAlignedAlloc(ALIGNMENT, dataCapacity);
      
      savedAddresses = new long[stackCapacity];
      position = 0;
   }
   
   public void push(){
      savedAddresses[position++] = address; 
   }
   
   public void pop(){
      address = savedAddresses[--position];
   }
   
   public long nalloc(int length){
      long result = address;
      address += length;
      return result;
   }
   
   public long nallocChar(int length){
      align(2);
      long result = address;
      address += length << 1;
      return result;
   }
   
   public long nallocShort(int length){
      align(2);
      long result = address;
      address += length << 1;
      return result;
   }
   
   public long nallocInt(int length){
      align(4);
      long result = address;
      address += length << 2;
      return result;
   }
   
   public long nallocFloat(int length){
      align(4);
      long result = address;
      address += length << 2;
      return result;
   }
   
   public long nallocLong(int length){
      align(8);
      long result = address;
      address += length << 3;
      return result;
   }
   
   public long nallocDouble(int length){
      align(8);
      long result = address;
      address += length << 3;
      return result;
   }
   
   public long nallocPointer(int length){
      align(Pointer.POINTER_SIZE);
      long result = address;
      address += length << Pointer.POINTER_SHIFT;
      return result;
   }
   
   public ByteBuffer alloc(int length){
      return MemoryUtil.memByteBuffer(nalloc(length), length);
   }
   
   public CharBuffer allocChar(int length){
      return MemoryUtil.memCharBuffer(nallocChar(length), length);
   }
   
   public ShortBuffer allocShort(int length){
      return MemoryUtil.memShortBuffer(nallocShort(length), length);
   }
   
   public IntBuffer allocInt(int length){
      return MemoryUtil.memIntBuffer(nallocInt(length), length);
   }
   
   public FloatBuffer allocFloat(int length){
      return MemoryUtil.memFloatBuffer(nallocFloat(length), length);
   }
   
   public LongBuffer allocLong(int length){
      return MemoryUtil.memLongBuffer(nallocLong(length), length);
   }
   
   public DoubleBuffer allocDouble(int length){
      return MemoryUtil.memDoubleBuffer(nallocDouble(length), length);
   }
   
   public PointerBuffer allocPointer(int length){
      return MemoryUtil.memPointerBuffer(nallocPointer(length), length);
   }

   private void align(int alignment) {
      long n = alignment - 1;
      address = (address + n) & (~n);
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
Riven (579 views)
2019-09-04 15:33:17

hadezbladez (5501 views)
2018-11-16 13:46:03

hadezbladez (2398 views)
2018-11-16 13:41:33

hadezbladez (5761 views)
2018-11-16 13:35:35

hadezbladez (1220 views)
2018-11-16 13:32:03

EgonOlsen (4660 views)
2018-06-10 19:43:48

EgonOlsen (5681 views)
2018-06-10 19:43:44

EgonOlsen (3196 views)
2018-06-10 19:43:20

DesertCoockie (4094 views)
2018-05-13 18:23:11

nelsongames (5114 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!