Skip to content

Commit 18ecc02

Browse files
author
zhangjipeng
committed
feat(memory): add memory manager for backport interface
Signed-off-by: zhangjipeng <zhangjipeng@xiaomi.com>
1 parent 372e693 commit 18ecc02

25 files changed

Lines changed: 285 additions & 75 deletions

include/include.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (C) 2025 Zhang Ji Peng
44
# Contact: onecoolx@gmail.com
55

6-
file(GLOB_RECURSE PICASSO_HEADERS ${PROJECT_ROOT}/include/picasso.h ${PROJECT_ROOT}/include/picasso_ext.h)
6+
file(GLOB_RECURSE PICASSO_HEADERS ${PROJECT_ROOT}/include/picasso.h ${PROJECT_ROOT}/include/picasso_backport.h ${PROJECT_ROOT}/include/picasso_ext.h)
77
install(FILES ${PICASSO_HEADERS} DESTINATION include/picasso)
88

99
file(GLOB_RECURSE PSX_IMAGES_HEADERS ${PROJECT_ROOT}/include/images/*.h)

include/picasso_backport.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* \file picasso_backport.h
3+
* \author Zhang Ji Peng <onecoolx@gmail.com>
4+
* \date 2026/1/17
5+
*
6+
* This file includes all interfaces of Picasso
7+
*
8+
\verbatim
9+
10+
Copyright (C) 2008 ~ 2026 Zhang Ji Peng
11+
12+
All rights reserved.
13+
14+
Picasso is a vector graphic library.
15+
16+
\endverbatim
17+
*/
18+
19+
#ifndef _PICASSO_BACKPORT_H_
20+
#define _PICASSO_BACKPORT_H_
21+
22+
#include "picasso.h"
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif /* __cplusplus */
27+
28+
/**
29+
* \defgroup backport Backport interface
30+
* @{
31+
*/
32+
33+
/**
34+
* \defgroup memory Memory Management
35+
* @{
36+
*/
37+
38+
/**
39+
* \brief Memory allocation function pointer type
40+
* \param size Size in bytes to allocate
41+
* \return Pointer to allocated memory or NULL on failure
42+
*/
43+
typedef void* (*ps_malloc_func)(size_t size);
44+
45+
/**
46+
* \brief Memory free function pointer type
47+
* \param ptr Pointer to memory to free
48+
*/
49+
typedef void (*ps_free_func)(void* ptr);
50+
51+
/**
52+
* \brief Memory calloc function pointer type
53+
* \param num Number of elements
54+
* \param size Size of each element in bytes
55+
* \return Pointer to allocated memory or NULL on failure
56+
*/
57+
typedef void* (*ps_calloc_func)(size_t num, size_t size);
58+
59+
/**
60+
* \brief Memory allocator functions structure
61+
*/
62+
typedef struct _ps_memory_funcs {
63+
ps_malloc_func mem_malloc; /** Memory allocation function */
64+
ps_free_func mem_free; /** Memory free function */
65+
ps_calloc_func mem_calloc; /** Memory calloc function */
66+
} ps_memory_funcs;
67+
68+
/** @} end of memory */
69+
70+
/** @} end of backport */
71+
72+
#ifdef __cplusplus
73+
}
74+
#endif /* __cplusplus */
75+
76+
#endif /*_PICASSO_BACKPORT_H_*/

src/core/memory_manager.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2026, Zhang Ji Peng
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
#include "common.h"
28+
29+
#include "global.h"
30+
#include "memory_manager.h"
31+
32+
namespace picasso {
33+
34+
#ifdef __cplusplus
35+
extern "C" {
36+
#endif /* __cplusplus */
37+
38+
void* _internal_malloc(size_t size)
39+
{
40+
return malloc(size);
41+
}
42+
43+
void _internal_free(void* ptr)
44+
{
45+
return free(ptr);
46+
}
47+
48+
void* _internal_calloc(size_t num, size_t size)
49+
{
50+
return calloc(num, size);
51+
}
52+
53+
#ifdef __cplusplus
54+
}
55+
#endif /* __cplusplus */
56+
57+
struct global_data _global = {
58+
._malloc = _internal_malloc,
59+
._free = _internal_free,
60+
._calloc = _internal_calloc
61+
};
62+
63+
} // namespace picasso

src/font/font_load_freetype2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626

2727
#include "common.h"
28-
#include "picasso_global.h"
28+
#include "picasso_private.h"
2929

3030
#if ENABLE(FREE_TYPE2)
3131
#include <ft2build.h>

src/include/global.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2026, Zhang Ji Peng
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
#ifndef _GLOBAL_H_
28+
#define _GLOBAL_H_
29+
30+
namespace picasso {
31+
32+
typedef void* (*malloc_func)(size_t size);
33+
typedef void (*free_func)(void* ptr);
34+
typedef void* (*calloc_func)(size_t num, size_t size);
35+
36+
struct global_data {
37+
malloc_func _malloc;
38+
free_func _free;
39+
calloc_func _calloc;
40+
};
41+
42+
extern struct global_data _global;
43+
44+
} // namespace picasso
45+
46+
#endif /*_GLOBAL_H_*/

src/include/math_type.h

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
1-
/* Picasso - a vector graphics library
1+
/*
2+
* Copyright (c) 2026, Zhang Ji Peng
3+
* All rights reserved.
24
*
3-
* Copyright (C) 2010 Zhang Ji Peng
4-
* Contact: onecoolx@gmail.com
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
525
*/
626

727
#ifndef _MATH_TYPES_H_
@@ -38,9 +58,10 @@ typedef float scalar;
3858
#define Round(x) roundf(x)
3959

4060
#define SqrtD(x) sqrt(x)
61+
4162
// max min
42-
template <typename T> inline T Min(T a, T b) { return (a < b) ? a : b; }
43-
template <typename T> inline T Max(T a, T b) { return (a > b) ? a : b; }
63+
#define Max(x, y) (((x) > (y)) ? (x) : (y))
64+
#define Min(x, y) (((x) < (y)) ? (x) : (y))
4465

4566
// float type
4667
inline uint32_t _uround(float v)

src/include/memory_manager.h

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
1-
/* Picasso - a vector graphics library
1+
/*
2+
* Copyright (c) 2026, Zhang Ji Peng
3+
* All rights reserved.
24
*
3-
* Copyright (C) 2010 Zhang Ji Peng
4-
* Contact: onecoolx@gmail.com
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
525
*/
626

727
#ifndef _MEMORY_MANAGER_H_
@@ -11,17 +31,24 @@
1131
#include <stdlib.h>
1232
#include <stdint.h>
1333

34+
#include "global.h"
1435
#include "fastcopy.h"
1536

1637
// common memory managers
1738

18-
#define mem_malloc(n) malloc(n)
19-
#define mem_calloc(n, s) calloc(n, s)
20-
#define mem_free(p) free(p)
39+
#define mem_malloc(n) (picasso::_global._malloc((n)))
40+
#define mem_calloc(n, s) (picasso::_global._calloc((n), (s)))
41+
#define mem_free(p) (picasso::_global._free((p)))
2142

2243
#define mem_deep_copy(d, s, l) memmove(d, s, l)
2344
#define mem_copy(d, s, l) fastcopy(d, s, l)
2445

46+
// this can be replace by hw buffer!
47+
#define BufferAlloc(n) mem_calloc(n, 1)
48+
#define BuffersAlloc(n, s) mem_calloc(n, s)
49+
#define BufferFree(p) mem_free(p)
50+
#define BufferCopy(d, s, n) mem_copy(d, s, n)
51+
2552
#if !ENABLE(SYSTEM_MALLOC) && !COMPILER(CLANG)
2653
#undef new
2754
#undef delete

src/picasso_api.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "convert.h"
1212

1313
#include "picasso.h"
14-
#include "picasso_global.h"
1514
#include "picasso_objects.h"
1615
#include "picasso_painter.h"
1716
#include "picasso_private.h"

src/picasso_canvas.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
*/
66

77
#include "common.h"
8+
#include "convert.h"
89
#include "device.h"
910
#include "graphic_path.h"
1011
#include "geometry.h"
11-
#include "convert.h"
12+
#include "global.h"
1213

1314
#include "picasso.h"
14-
#include "picasso_global.h"
1515
#include "picasso_objects.h"
1616
#include "picasso_painter.h"
1717
#include "picasso_private.h"
@@ -363,10 +363,10 @@ ps_canvas* PICAPI ps_canvas_create_from_mask(ps_mask* m, const ps_rect* r)
363363
rc.y = r->y;
364364
}
365365
if (r->w > 0) {
366-
rc.w = MIN(rc.w - rc.x, r->w);
366+
rc.w = Min(rc.w - rc.x, r->w);
367367
}
368368
if (r->h > 0) {
369-
rc.h = MIN(rc.h - rc.y, r->h);
369+
rc.h = Min(rc.h - rc.y, r->h);
370370
}
371371
}
372372

src/picasso_font.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "geometry.h"
1212
#include "convert.h"
1313

14-
#include "picasso_global.h"
1514
#include "picasso_private.h"
1615
#include "picasso_painter.h"
1716
#include "picasso_font.h"

0 commit comments

Comments
 (0)