11/*
2- * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3- * (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
2+ * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3+ * (C) 2025 Vladimir Sadovnikov <sadko4u@gmail.com>
44 *
55 * This file is part of lsp-common-lib
66 * Created on: 3 апр. 2020 г.
@@ -30,50 +30,64 @@ namespace lsp
3030{
3131 inline size_t align_size (size_t size, size_t align)
3232 {
33- size_t off = size % align;
33+ const size_t off = size % align;
3434 return (off) ? (size + align - off) : size;
3535 }
3636
3737 template <class T >
38- inline T *align_ptr (T *src, size_t align = DEFAULT_ALIGN)
39- {
40- uintptr_t x = uintptr_t (src);
41- uintptr_t off = x % align;
42- return (off) ?
43- reinterpret_cast <T *>(x + align - off) :
44- src;
45- }
38+ inline T *align_ptr (T *src, size_t align = DEFAULT_ALIGN)
39+ {
40+ const uintptr_t x = uintptr_t (src);
41+ const uintptr_t off = x % align;
42+ return (off) ?
43+ reinterpret_cast <T *>(x + align - off) :
44+ src;
45+ }
4646
4747 template <class T >
48- inline bool is_ptr_aligned (T *src, size_t align = DEFAULT_ALIGN)
49- {
50- ptrdiff_t x = ptrdiff_t (src);
51- return !(x % align);
52- }
48+ inline bool is_ptr_aligned (T *src, size_t align = DEFAULT_ALIGN)
49+ {
50+ ptrdiff_t x = ptrdiff_t (src);
51+ return !(x % align);
52+ }
5353
5454 template <class T >
55- inline T *lsp_malloc (size_t count = 1 )
56- {
57- return static_cast <T *>(::malloc (sizeof (T) * count));
58- }
55+ inline T *lsp_malloc (size_t count = 1 )
56+ {
57+ return static_cast <T *>(::malloc (sizeof (T) * count));
58+ }
59+
60+ template <class T , class P >
61+ inline T *advance_ptr (P * &ptr, size_t count)
62+ {
63+ const uintptr_t x = uintptr_t (ptr);
64+ T * const result = reinterpret_cast <T *>(ptr);
65+ ptr = reinterpret_cast <P *>(x + count * sizeof (T));
66+ return result;
67+ }
68+
69+ template <class T , class P >
70+ inline T *advance_ptr_bytes (P * &ptr, size_t count)
71+ {
72+ const uintptr_t x = uintptr_t (ptr);
73+ T * const result = reinterpret_cast <T *>(ptr);
74+ ptr = reinterpret_cast <P *>(x + count);
75+ return result;
76+ }
5977
6078 template <class T , class P >
61- inline T *advance_ptr (P * &ptr, size_t count)
62- {
63- uintptr_t x = uintptr_t (ptr);
64- T *result = reinterpret_cast <T *>(ptr);
65- ptr = reinterpret_cast <P *>(x + count * sizeof (T));
66- return result;
67- }
79+ inline T *add_ptr (P * ptr, size_t count)
80+ {
81+ const uintptr_t x = uintptr_t (ptr);
82+ return reinterpret_cast <T *>(x + count * sizeof (T));
83+ }
6884
6985 template <class T , class P >
70- inline T *advance_ptr_bytes (P * &ptr, size_t count)
71- {
72- uintptr_t x = uintptr_t (ptr);
73- T *result = reinterpret_cast <T *>(ptr);
74- ptr = reinterpret_cast <P *>(x + count);
75- return result;
76- }
86+ inline T *add_ptr_bytes (P * &ptr, size_t count)
87+ {
88+ const uintptr_t x = uintptr_t (ptr);
89+ return reinterpret_cast <T *>(x + count);
90+ }
7791
7892 /* * Allocate aligned pointer
7993 *
@@ -91,25 +105,25 @@ namespace lsp
91105 * a = NULL;
92106 */
93107 template <class T , class P >
94- inline T *alloc_aligned (P * &ptr, size_t count, size_t align=DEFAULT_ALIGN)
95- {
96- // Check for power of 2
97- if ((!align) || (align & (align-1 )))
98- return NULL ;
99-
100- // Allocate data
101- void *p = ::malloc ((count * sizeof (T)) + align);
102- if (p == NULL )
103- return NULL ;
104-
105- // Store pointer
106- ptr = reinterpret_cast <P *>(p);
107-
108- // Return aligned pointer
109- ptrdiff_t x = ptrdiff_t (p);
110- ptrdiff_t mask = align-1 ;
111- return reinterpret_cast <T *>((x & mask) ? ((x + align)&(~mask)) : x);
112- }
108+ inline T *alloc_aligned (P * &ptr, size_t count, size_t align=DEFAULT_ALIGN)
109+ {
110+ // Check for power of 2
111+ if ((!align) || (align & (align-1 )))
112+ return NULL ;
113+
114+ // Allocate data
115+ void * const p = ::malloc ((count * sizeof (T)) + align);
116+ if (p == NULL )
117+ return NULL ;
118+
119+ // Store pointer
120+ ptr = reinterpret_cast <P *>(p);
121+
122+ // Return aligned pointer
123+ const ptrdiff_t x = ptrdiff_t (p);
124+ const ptrdiff_t mask = align-1 ;
125+ return reinterpret_cast <T *>((x & mask) ? ((x + align)&(~mask)) : x);
126+ }
113127
114128 /* * Reallocate aligned pointer. Allocate new memory chunk if pointer was not previously allocated.
115129 *
@@ -127,39 +141,39 @@ namespace lsp
127141 * a = NULL;
128142 */
129143 template <class T , class P >
130- inline T *realloc_aligned (P * &ptr, size_t count, size_t align=DEFAULT_ALIGN)
131- {
132- // Check for power of 2
133- if ((!align) || (align & (align-1 )))
134- return NULL ;
135-
136- // Allocate data
137- void *p = ::realloc (ptr, (count * sizeof (T)) + align);
138- if (p == NULL )
139- return NULL ;
140-
141- // Store pointer
142- ptr = reinterpret_cast <P *>(p);
143-
144- // Return aligned pointer
145- ptrdiff_t x = ptrdiff_t (p);
146- ptrdiff_t mask = align- 1 ;
147- return reinterpret_cast <T *>((x & mask) ? ((x + align)&(~mask)) : x);
148- }
144+ inline T *realloc_aligned (P * &ptr, size_t count, size_t align=DEFAULT_ALIGN)
145+ {
146+ // Check for power of 2
147+ if ((!align) || (align & (align-1 )))
148+ return NULL ;
149+
150+ // Allocate data
151+ void * const p = ::realloc (ptr, (count * sizeof (T)) + align);
152+ if (p == NULL )
153+ return NULL ;
154+
155+ // Store pointer
156+ ptr = reinterpret_cast <P *>(p);
157+
158+ // Return aligned pointer
159+ const ptrdiff_t x = ptrdiff_t (p);
160+ const ptrdiff_t mask = align - 1 ;
161+ return reinterpret_cast <T *>((x & mask) ? ((x + align)&(~mask)) : x);
162+ }
149163
150164 /* * Free aligned pointer and write NULL to it
151165 *
152166 * @param ptr pointer to free
153167 */
154168 template <class P >
155- inline void free_aligned (P * &ptr)
156- {
157- if (ptr == NULL )
158- return ;
159- P *tptr = ptr;
160- ptr = NULL ;
161- ::free (tptr);
162- }
169+ inline void free_aligned (P * &ptr)
170+ {
171+ if (ptr == NULL )
172+ return ;
173+ P * const tptr = ptr;
174+ ptr = NULL ;
175+ ::free (tptr);
176+ }
163177
164178 /* *
165179 * Seed the address
0 commit comments