@@ -34,6 +34,12 @@ extern "C" {
34
34
35
35
#define FONS_INVALID -1
36
36
37
+ #if !defined(FONTSTASH_MALLOC )
38
+ #define FONTSTASH_MALLOC malloc
39
+ #define FONTSTASH_REALLOC realloc
40
+ #define FONTSTASH_FREE free
41
+ #endif
42
+
37
43
enum FONSflags {
38
44
FONS_ZERO_TOPLEFT = 1 ,
39
45
FONS_ZERO_BOTTOMLEFT = 2 ,
@@ -540,24 +546,24 @@ static unsigned int fons__decutf8(unsigned int* state, unsigned int* codep, unsi
540
546
static void fons__deleteAtlas (FONSatlas * atlas )
541
547
{
542
548
if (atlas == NULL ) return ;
543
- if (atlas -> nodes != NULL ) free (atlas -> nodes );
544
- free (atlas );
549
+ if (atlas -> nodes != NULL ) FONTSTASH_FREE (atlas -> nodes );
550
+ FONTSTASH_FREE (atlas );
545
551
}
546
552
547
553
static FONSatlas * fons__allocAtlas (int w , int h , int nnodes )
548
554
{
549
555
FONSatlas * atlas = NULL ;
550
556
551
557
// Allocate memory for the font stash.
552
- atlas = (FONSatlas * )malloc (sizeof (FONSatlas ));
558
+ atlas = (FONSatlas * )FONTSTASH_MALLOC (sizeof (FONSatlas ));
553
559
if (atlas == NULL ) goto error ;
554
560
memset (atlas , 0 , sizeof (FONSatlas ));
555
561
556
562
atlas -> width = w ;
557
563
atlas -> height = h ;
558
564
559
565
// Allocate space for skyline nodes
560
- atlas -> nodes = (FONSatlasNode * )malloc (sizeof (FONSatlasNode ) * nnodes );
566
+ atlas -> nodes = (FONSatlasNode * )FONTSTASH_MALLOC (sizeof (FONSatlasNode ) * nnodes );
561
567
if (atlas -> nodes == NULL ) goto error ;
562
568
memset (atlas -> nodes , 0 , sizeof (FONSatlasNode ) * nnodes );
563
569
atlas -> nnodes = 0 ;
@@ -582,7 +588,7 @@ static int fons__atlasInsertNode(FONSatlas* atlas, int idx, int x, int y, int w)
582
588
// Insert node
583
589
if (atlas -> nnodes + 1 > atlas -> cnodes ) {
584
590
atlas -> cnodes = atlas -> cnodes == 0 ? 8 : atlas -> cnodes * 2 ;
585
- atlas -> nodes = (FONSatlasNode * )realloc (atlas -> nodes , sizeof (FONSatlasNode ) * atlas -> cnodes );
591
+ atlas -> nodes = (FONSatlasNode * )FONTSTASH_REALLOC (atlas -> nodes , sizeof (FONSatlasNode ) * atlas -> cnodes );
586
592
if (atlas -> nodes == NULL )
587
593
return 0 ;
588
594
}
@@ -743,14 +749,14 @@ FONScontext* fonsCreateInternal(FONSparams* params)
743
749
FONScontext * stash = NULL ;
744
750
745
751
// Allocate memory for the font stash.
746
- stash = (FONScontext * )malloc (sizeof (FONScontext ));
752
+ stash = (FONScontext * )FONTSTASH_MALLOC (sizeof (FONScontext ));
747
753
if (stash == NULL ) goto error ;
748
754
memset (stash , 0 , sizeof (FONScontext ));
749
755
750
756
stash -> params = * params ;
751
757
752
758
// Allocate scratch buffer.
753
- stash -> scratch = (unsigned char * )malloc (FONS_SCRATCH_BUF_SIZE );
759
+ stash -> scratch = (unsigned char * )FONTSTASH_MALLOC (FONS_SCRATCH_BUF_SIZE );
754
760
if (stash -> scratch == NULL ) goto error ;
755
761
756
762
// Initialize implementation library
@@ -765,7 +771,7 @@ FONScontext* fonsCreateInternal(FONSparams* params)
765
771
if (stash -> atlas == NULL ) goto error ;
766
772
767
773
// Allocate space for fonts.
768
- stash -> fonts = (FONSfont * * )malloc (sizeof (FONSfont * ) * FONS_INIT_FONTS );
774
+ stash -> fonts = (FONSfont * * )FONTSTASH_MALLOC (sizeof (FONSfont * ) * FONS_INIT_FONTS );
769
775
if (stash -> fonts == NULL ) goto error ;
770
776
memset (stash -> fonts , 0 , sizeof (FONSfont * ) * FONS_INIT_FONTS );
771
777
stash -> cfonts = FONS_INIT_FONTS ;
@@ -774,7 +780,7 @@ FONScontext* fonsCreateInternal(FONSparams* params)
774
780
// Create texture for the cache.
775
781
stash -> itw = 1.0f /stash -> params .width ;
776
782
stash -> ith = 1.0f /stash -> params .height ;
777
- stash -> texData = (unsigned char * )malloc (stash -> params .width * stash -> params .height );
783
+ stash -> texData = (unsigned char * )FONTSTASH_MALLOC (stash -> params .width * stash -> params .height );
778
784
if (stash -> texData == NULL ) goto error ;
779
785
memset (stash -> texData , 0 , stash -> params .width * stash -> params .height );
780
786
@@ -877,25 +883,25 @@ void fonsClearState(FONScontext* stash)
877
883
static void fons__freeFont (FONSfont * font )
878
884
{
879
885
if (font == NULL ) return ;
880
- if (font -> glyphs ) free (font -> glyphs );
881
- if (font -> freeData && font -> data ) free (font -> data );
882
- free (font );
886
+ if (font -> glyphs ) FONTSTASH_FREE (font -> glyphs );
887
+ if (font -> freeData && font -> data ) FONTSTASH_FREE (font -> data );
888
+ FONTSTASH_FREE (font );
883
889
}
884
890
885
891
static int fons__allocFont (FONScontext * stash )
886
892
{
887
893
FONSfont * font = NULL ;
888
894
if (stash -> nfonts + 1 > stash -> cfonts ) {
889
895
stash -> cfonts = stash -> cfonts == 0 ? 8 : stash -> cfonts * 2 ;
890
- stash -> fonts = (FONSfont * * )realloc (stash -> fonts , sizeof (FONSfont * ) * stash -> cfonts );
896
+ stash -> fonts = (FONSfont * * )FONTSTASH_REALLOC (stash -> fonts , sizeof (FONSfont * ) * stash -> cfonts );
891
897
if (stash -> fonts == NULL )
892
898
return -1 ;
893
899
}
894
- font = (FONSfont * )malloc (sizeof (FONSfont ));
900
+ font = (FONSfont * )FONTSTASH_MALLOC (sizeof (FONSfont ));
895
901
if (font == NULL ) goto error ;
896
902
memset (font , 0 , sizeof (FONSfont ));
897
903
898
- font -> glyphs = (FONSglyph * )malloc (sizeof (FONSglyph ) * FONS_INIT_GLYPHS );
904
+ font -> glyphs = (FONSglyph * )FONTSTASH_MALLOC (sizeof (FONSglyph ) * FONS_INIT_GLYPHS );
899
905
if (font -> glyphs == NULL ) goto error ;
900
906
font -> cglyphs = FONS_INIT_GLYPHS ;
901
907
font -> nglyphs = 0 ;
@@ -967,7 +973,7 @@ static FONSglyph* fons__allocGlyph(FONSfont* font)
967
973
{
968
974
if (font -> nglyphs + 1 > font -> cglyphs ) {
969
975
font -> cglyphs = font -> cglyphs == 0 ? 8 : font -> cglyphs * 2 ;
970
- font -> glyphs = (FONSglyph * )realloc (font -> glyphs , sizeof (FONSglyph ) * font -> cglyphs );
976
+ font -> glyphs = (FONSglyph * )FONTSTASH_REALLOC (font -> glyphs , sizeof (FONSglyph ) * font -> cglyphs );
971
977
if (font -> glyphs == NULL ) return NULL ;
972
978
}
973
979
font -> nglyphs ++ ;
@@ -1610,10 +1616,10 @@ FONS_DEF void fonsDeleteInternal(FONScontext* stash)
1610
1616
fons__freeFont (stash -> fonts [i ]);
1611
1617
1612
1618
if (stash -> atlas ) fons__deleteAtlas (stash -> atlas );
1613
- if (stash -> fonts ) free (stash -> fonts );
1614
- if (stash -> texData ) free (stash -> texData );
1615
- if (stash -> scratch ) free (stash -> scratch );
1616
- free (stash );
1619
+ if (stash -> fonts ) FONTSTASH_FREE (stash -> fonts );
1620
+ if (stash -> texData ) FONTSTASH_FREE (stash -> texData );
1621
+ if (stash -> scratch ) FONTSTASH_FREE (stash -> scratch );
1622
+ FONTSTASH_FREE (stash );
1617
1623
}
1618
1624
1619
1625
FONS_DEF void fonsSetErrorCallback (FONScontext * stash , void (* callback )(void * uptr , int error , int val ), void * uptr )
@@ -1651,7 +1657,7 @@ FONS_DEF int fonsExpandAtlas(FONScontext* stash, int width, int height)
1651
1657
return 0 ;
1652
1658
}
1653
1659
// Copy old texture data over.
1654
- data = (unsigned char * )malloc (width * height );
1660
+ data = (unsigned char * )FONTSTASH_MALLOC (width * height );
1655
1661
if (data == NULL )
1656
1662
return 0 ;
1657
1663
for (i = 0 ; i < stash -> params .height ; i ++ ) {
@@ -1664,7 +1670,7 @@ FONS_DEF int fonsExpandAtlas(FONScontext* stash, int width, int height)
1664
1670
if (height > stash -> params .height )
1665
1671
memset (& data [stash -> params .height * width ], 0 , (height - stash -> params .height ) * width );
1666
1672
1667
- free (stash -> texData );
1673
+ FONTSTASH_FREE (stash -> texData );
1668
1674
stash -> texData = data ;
1669
1675
1670
1676
// Increase atlas size
@@ -1704,7 +1710,7 @@ FONS_DEF int fonsResetAtlas(FONScontext* stash, int width, int height)
1704
1710
fons__atlasReset (stash -> atlas , width , height );
1705
1711
1706
1712
// Clear texture data.
1707
- stash -> texData = (unsigned char * )realloc (stash -> texData , width * height );
1713
+ stash -> texData = (unsigned char * )FONTSTASH_REALLOC (stash -> texData , width * height );
1708
1714
if (stash -> texData == NULL ) return 0 ;
1709
1715
memset (stash -> texData , 0 , width * height );
1710
1716
0 commit comments