Skip to content

Commit de7abd2

Browse files
committed
c++: add parameter to request zero-initialised buffers
Supposedly later jellyfin patches won't always need zero-initialised memory, so we can't just do this unconditionally. I doubt this is in any relevant way faster than an external memset, but there was a strong push against redoing the multiplication or just zeroing the actual capacity.
1 parent 4fdbddb commit de7abd2

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/SubtitleOctopus.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ class ReusableBuffer2D {
4343
/*
4444
* Request a raw pointer to a buffer being able to hold at least
4545
* x times y values of size member_size.
46+
* If zero is set to true, the requested region will be zero-initialised.
4647
* On failure NULL is returned.
4748
* The pointer is valid during the lifetime of the ReusableBuffer
4849
* object until the next call to get_rawbuf or clear.
4950
*/
50-
void *get_rawbuf(size_t x, size_t y, size_t member_size) {
51+
void *get_rawbuf(size_t x, size_t y, size_t member_size, bool zero) {
5152
if (x > SIZE_MAX / member_size / y)
5253
return NULL;
5354

@@ -62,15 +63,18 @@ class ReusableBuffer2D {
6263
}
6364
if (lessen_counter < 10) {
6465
// not reducing the buffer yet
66+
if (zero)
67+
memset(buffer, 0, new_size);
6568
return buffer;
6669
}
6770
}
6871

6972
free(buffer);
7073
buffer = malloc(new_size);
71-
if (buffer)
74+
if (buffer) {
7275
size = new_size;
73-
else
76+
memset(buffer, 0, size);
77+
} else
7478
size = 0;
7579
lessen_counter = 0;
7680
return buffer;
@@ -281,12 +285,11 @@ class SubtitleOctopus {
281285
}
282286

283287
// make float buffer for blending
284-
float* buf = (float*)m_blend.get_rawbuf(width, height, sizeof(float) * 4);
288+
float* buf = (float*)m_blend.get_rawbuf(width, height, sizeof(float) * 4, true);
285289
if (buf == NULL) {
286290
fprintf(stderr, "jso: cannot allocate buffer for blending\n");
287291
return &m_blendResult;
288292
}
289-
memset(buf, 0, sizeof(float) * width * height * 4);
290293

291294
// blend things in
292295
for (cur = img; cur != NULL; cur = cur->next) {

0 commit comments

Comments
 (0)