@@ -33,48 +33,74 @@ public static int getAllocCount() {
3333 return allocCount ;
3434 }
3535
36+ private static final ByteBuffer ZERO_LENGTH_BYTE_BUFFER = new EaglerLWJGLByteBuffer (0l , 0 , true );
37+
3638 public static ByteBuffer allocByteBuffer (int len ) {
37- long ret = JEmalloc .nje_malloc (len );
38- if (ret == 0l ) {
39- throw new OutOfMemoryError ("Native je_malloc call returned null pointer!" );
39+ if (len != 0 ) {
40+ long ret = JEmalloc .nje_malloc (len );
41+ if (ret == 0l ) {
42+ throw new OutOfMemoryError ("Native je_malloc call returned null pointer!" );
43+ }
44+ if (enableAllocCount ) ++allocCount ;
45+ return new EaglerLWJGLByteBuffer (ret , len , true );
46+ }else {
47+ return ZERO_LENGTH_BYTE_BUFFER ;
4048 }
41- if (enableAllocCount ) ++allocCount ;
42- return new EaglerLWJGLByteBuffer (ret , len , true );
4349 }
4450
51+ private static final ShortBuffer ZERO_LENGTH_SHORT_BUFFER = new EaglerLWJGLShortBuffer (0l , 0 , true );
52+
4553 public static ShortBuffer allocShortBuffer (int len ) {
46- long ret = JEmalloc .nje_malloc (len << 1 );
47- if (ret == 0l ) {
48- throw new OutOfMemoryError ("Native je_malloc call returned null pointer!" );
54+ if (len != 0 ) {
55+ long ret = JEmalloc .nje_malloc (len << 1 );
56+ if (ret == 0l ) {
57+ throw new OutOfMemoryError ("Native je_malloc call returned null pointer!" );
58+ }
59+ if (enableAllocCount ) ++allocCount ;
60+ return new EaglerLWJGLShortBuffer (ret , len , true );
61+ }else {
62+ return ZERO_LENGTH_SHORT_BUFFER ;
4963 }
50- if (enableAllocCount ) ++allocCount ;
51- return new EaglerLWJGLShortBuffer (ret , len , true );
5264 }
5365
66+ private static final IntBuffer ZERO_LENGTH_INT_BUFFER = new EaglerLWJGLIntBuffer (0l , 0 , true );
67+
5468 public static IntBuffer allocIntBuffer (int len ) {
55- long ret = JEmalloc .nje_malloc (len << 2 );
56- if (ret == 0l ) {
57- throw new OutOfMemoryError ("Native je_malloc call returned null pointer!" );
69+ if (len != 0 ) {
70+ long ret = JEmalloc .nje_malloc (len << 2 );
71+ if (ret == 0l ) {
72+ throw new OutOfMemoryError ("Native je_malloc call returned null pointer!" );
73+ }
74+ if (enableAllocCount ) ++allocCount ;
75+ return new EaglerLWJGLIntBuffer (ret , len , true );
76+ }else {
77+ return ZERO_LENGTH_INT_BUFFER ;
5878 }
59- if (enableAllocCount ) ++allocCount ;
60- return new EaglerLWJGLIntBuffer (ret , len , true );
6179 }
6280
81+ private static final FloatBuffer ZERO_LENGTH_FLOAT_BUFFER = new EaglerLWJGLFloatBuffer (0l , 0 , true );
82+
6383 public static FloatBuffer allocFloatBuffer (int len ) {
64- long ret = JEmalloc .nje_malloc (len << 2 );
65- if (ret == 0l ) {
66- throw new OutOfMemoryError ("Native je_malloc call returned null pointer!" );
84+ if (len != 0 ) {
85+ long ret = JEmalloc .nje_malloc (len << 2 );
86+ if (ret == 0l ) {
87+ throw new OutOfMemoryError ("Native je_malloc call returned null pointer!" );
88+ }
89+ if (enableAllocCount ) ++allocCount ;
90+ return new EaglerLWJGLFloatBuffer (ret , len , true );
91+ }else {
92+ return ZERO_LENGTH_FLOAT_BUFFER ;
6793 }
68- if (enableAllocCount ) ++allocCount ;
69- return new EaglerLWJGLFloatBuffer (ret , len , true );
7094 }
7195
7296 public static void freeByteBuffer (ByteBuffer buffer ) {
7397 if (buffer instanceof EaglerLWJGLByteBuffer ) {
7498 EaglerLWJGLByteBuffer buf = (EaglerLWJGLByteBuffer )buffer ;
7599 if (buf .original ) {
76- JEmalloc .nje_free (buf .address );
77- if (enableAllocCount ) --allocCount ;
100+ if (buf .address != 0l ) {
101+ JEmalloc .nje_free (buf .address );
102+ if (enableAllocCount ) --allocCount ;
103+ }
78104 }else {
79105 throwNotOriginal (buffer );
80106 }
@@ -96,8 +122,10 @@ public static void freeShortBuffer(ShortBuffer buffer) {
96122 if (buffer instanceof EaglerLWJGLShortBuffer ) {
97123 EaglerLWJGLShortBuffer buf = (EaglerLWJGLShortBuffer )buffer ;
98124 if (buf .original ) {
99- JEmalloc .nje_free (buf .address );
100- if (enableAllocCount ) --allocCount ;
125+ if (buf .address != 0l ) {
126+ JEmalloc .nje_free (buf .address );
127+ if (enableAllocCount ) --allocCount ;
128+ }
101129 }else {
102130 throwNotOriginal (buffer );
103131 }
@@ -119,8 +147,10 @@ public static void freeIntBuffer(IntBuffer buffer) {
119147 if (buffer instanceof EaglerLWJGLIntBuffer ) {
120148 EaglerLWJGLIntBuffer buf = (EaglerLWJGLIntBuffer )buffer ;
121149 if (buf .original ) {
122- JEmalloc .nje_free (buf .address );
123- if (enableAllocCount ) --allocCount ;
150+ if (buf .address != 0l ) {
151+ JEmalloc .nje_free (buf .address );
152+ if (enableAllocCount ) --allocCount ;
153+ }
124154 }else {
125155 throwNotOriginal (buffer );
126156 }
@@ -142,8 +172,10 @@ public static void freeFloatBuffer(FloatBuffer buffer) {
142172 if (buffer instanceof EaglerLWJGLFloatBuffer ) {
143173 EaglerLWJGLFloatBuffer buf = (EaglerLWJGLFloatBuffer )buffer ;
144174 if (buf .original ) {
145- JEmalloc .nje_free (buf .address );
146- if (enableAllocCount ) --allocCount ;
175+ if (buf .address != 0l ) {
176+ JEmalloc .nje_free (buf .address );
177+ if (enableAllocCount ) --allocCount ;
178+ }
147179 }else {
148180 throwNotOriginal (buffer );
149181 }
0 commit comments