Skip to content

Commit 9758969

Browse files
committed
Release 1.0.44
* Some improvement around endianess conversion functions. * Updated some basic definitions related to pointer allocation.
2 parents 3ab10f4 + 26d48f3 commit 9758969

File tree

6 files changed

+145
-100
lines changed

6 files changed

+145
-100
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
* RECENT CHANGES
33
*******************************************************************************
44

5+
=== 1.0.44 ===
6+
* Some improvement around endianess conversion functions.
7+
* Updated some basic definitions related to pointer allocation.
8+
59
=== 1.0.43 ===
610
* Better memory safety macros definition.
711
* Added MacOS CI builds.

include/lsp-plug.in/common/alloc.h

Lines changed: 94 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3-
* (C) 2020 Vladimir Sadovnikov <[email protected]>
2+
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3+
* (C) 2025 Vladimir Sadovnikov <[email protected]>
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

include/lsp-plug.in/common/endian.h

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3-
* (C) 2020 Vladimir Sadovnikov <[email protected]>
2+
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3+
* (C) 2025 Vladimir Sadovnikov <[email protected]>
44
*
55
* This file is part of lsp-common-lib
66
* Created on: 1 апр. 2020 г.
@@ -24,6 +24,7 @@
2424

2525
#include <lsp-plug.in/common/version.h>
2626
#include <lsp-plug.in/common/types.h>
27+
#include <lsp-plug.in/stdlib/string.h>
2728

2829
#define LSP_PLUG_IN_COMMON_ENDIAN_IMPL
2930
// Include unsigned functions definition
@@ -42,30 +43,42 @@
4243

4344
// Define macros
4445
#ifdef ARCH_LE
45-
#define LE_TO_CPU(x) (x)
46-
#define CPU_TO_LE(x) (x)
46+
#define LE_TO_CPU(x) (x)
47+
#define CPU_TO_LE(x) (x)
4748

48-
#define BE_TO_CPU(x) ::lsp::byte_swap(x)
49-
#define CPU_TO_BE(x) ::lsp::byte_swap(x)
49+
#define BE_TO_CPU(x) ::lsp::byte_swap(x)
50+
#define CPU_TO_BE(x) ::lsp::byte_swap(x)
5051

51-
#define VLE_TO_CPU(v, n) do {} while(false)
52-
#define CPU_TO_VLE(v, n) do {} while(false)
52+
#define VLE_TO_CPU(v, n) do {} while(false)
53+
#define CPU_TO_VLE(v, n) do {} while(false)
5354

54-
#define VBE_TO_CPU(v, n) ::lsp::byte_swap(v, n)
55-
#define CPU_TO_VBE(v, n) ::lsp::byte_swap(v, n)
55+
#define VBE_TO_CPU(v, n) ::lsp::byte_swap(v, n)
56+
#define CPU_TO_VBE(v, n) ::lsp::byte_swap(v, n)
57+
58+
#define VLE_TO_CPU_COPY(d, v, n) ::lsp::no_byte_swap_copy(d, v, n)
59+
#define CPU_TO_VLE_COPY(d, v, n) ::lsp::no_byte_swap_copy(d, v, n)
60+
61+
#define VBE_TO_CPU_COPY(d, v, n) ::lsp::byte_swap_copy(d, v, n)
62+
#define CPU_TO_VBE_COPY(d, v, n) ::lsp::byte_swap_copy(d, v, n)
5663

5764
#else
58-
#define LE_TO_CPU(x) ::lsp::byte_swap(x)
59-
#define CPU_TO_LE(x) ::lsp::byte_swap(x)
65+
#define LE_TO_CPU(x) ::lsp::byte_swap(x)
66+
#define CPU_TO_LE(x) ::lsp::byte_swap(x)
67+
68+
#define BE_TO_CPU(x) (x)
69+
#define CPU_TO_BE(x) (x)
70+
71+
#define VLE_TO_CPU(v, n) ::lsp::byte_swap(v, n)
72+
#define CPU_TO_VLE(v, n) ::lsp::byte_swap(v, n)
6073

61-
#define BE_TO_CPU(x) (x)
62-
#define CPU_TO_BE(x) (x)
74+
#define VBE_TO_CPU(v, n) do {} while(false)
75+
#define CPU_TO_VBE(v, n) do {} while(false)
6376

64-
#define VLE_TO_CPU(v, n) ::lsp::byte_swap(v, n)
65-
#define CPU_TO_VLE(v, n) ::lsp::byte_swap(v, n)
77+
#define VLE_TO_CPU_COPY(d, v, n) ::lsp::byte_swap_copy(d, v, n)
78+
#define CPU_TO_VLE_COPY(d, v, n) ::lsp::byte_swap_copy(d, v, n)
6679

67-
#define VBE_TO_CPU(v, n) do {} while(false)
68-
#define CPU_TO_VBE(v, n) do {} while(false)
80+
#define VBE_TO_CPU_COPY(d, v, n) ::lsp::no_byte_swap_copy(d, v, n)
81+
#define CPU_TO_VBE_COPY(d, v, n) ::lsp::no_byte_swap_copy(d, v, n)
6982

7083
#endif /* */
7184

@@ -121,6 +134,12 @@ namespace lsp
121134
for (size_t i=0; i<n; ++i)
122135
dst[i] = byte_swap(src[i]);
123136
}
137+
138+
template <class T>
139+
inline void no_byte_swap_copy(T *dst, const T *src, size_t n)
140+
{
141+
memcpy(dst, src, n * sizeof(T));
142+
}
124143
} /* namespace lsp */
125144

126145
#endif /* LSP_PLUG_IN_COMMON_ENDIAN_H_ */

include/lsp-plug.in/common/types.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,21 +777,25 @@ namespace lsp
777777
#ifdef ARCH_I386
778778
#define DEFAULT_ALIGN 0x10
779779
#define MINIMUM_ALIGN 0x08
780+
#define OPTIMAL_ALIGN 0x40
780781
#endif /* ARCH_X86 */
781782

782783
#ifdef ARCH_X86_64
783784
#define DEFAULT_ALIGN 0x10
784785
#define MINIMUM_ALIGN 0x08
786+
#define OPTIMAL_ALIGN 0x40
785787
#endif /* ARCH_X86 */
786788

787789
#ifdef ARCH_ARCH_ARM
788790
#define DEFAULT_ALIGN 0x10
789791
#define MINIMUM_ALIGN 0x08
792+
#define OPTIMAL_ALIGN 0x10
790793
#endif /* ARCH_ARM */
791794

792795
#ifdef ARCH_ARCH_AARCH64
793796
#define DEFAULT_ALIGN 0x10
794797
#define MINIMUM_ALIGN 0x08
798+
#define OPTIMAL_ALIGN 0x10
795799
#endif /* ARCH_ARM */
796800

797801
#ifndef DEFAULT_ALIGN
@@ -802,6 +806,10 @@ namespace lsp
802806
#define MINIMUM_ALIGN DEFAULT_ALIGN
803807
#endif /* DEFAULT_ALIGN */
804808

809+
#ifndef OPTIMAL_ALIGN
810+
#define OPTIMAL_ALIGN DEFAULT_ALIGN
811+
#endif /* DEFAULT_ALIGN */
812+
805813
#ifdef LSP_UNALIGNED_MEMORY_SAFE
806814
#define IF_UNALIGNED_MEMORY_SAFE(...) __VA_ARGS__
807815
#define IF_UNALIGNED_MEMORY_UNSAFE(...)

include/lsp-plug.in/common/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
// Version of headers
2727
#define LSP_COMMON_LIB_MAJOR 1
2828
#define LSP_COMMON_LIB_MINOR 0
29-
#define LSP_COMMON_LIB_MICRO 43
29+
#define LSP_COMMON_LIB_MICRO 44
3030

3131
#if defined(LSP_COMMON_LIB_PUBLISHER)
3232
#define LSP_COMMON_LIB_PUBLIC LSP_EXPORT_MODIFIER

project.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ ARTIFACT_ID = LSP_COMMON_LIB
2323
ARTIFACT_NAME = lsp-common-lib
2424
ARTIFACT_DESC = Common library for basic C/C++ language definitions
2525
ARTIFACT_HEADERS = lsp-plug.in
26-
ARTIFACT_VERSION = 1.0.43
26+
ARTIFACT_VERSION = 1.0.44
2727

0 commit comments

Comments
 (0)