Skip to content

Commit 9ccaadd

Browse files
committed
Catalog class update
1 parent a771107 commit 9ccaadd

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
lines changed

include/lsp-plug.in/dsp-units/shared/Catalog.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -159,22 +159,31 @@ namespace lsp
159159
public:
160160
/**
161161
* Create catalog record. If record already exists, it will be replaced.
162+
* @param record pointer to store catalog record
162163
* @param magic record type
163164
* @param name unique record name (UTF-8 encoded string)
164165
* @param id associated shared segment identifier (UTF-8 encoded string)
165166
* @return record index or negative error code
166167
*/
167-
ssize_t publish(uint32_t magic, const char *name, const char *id);
168+
ssize_t publish(Record *record, uint32_t magic, const char *name, const char *id);
168169

169170
/**
170171
* Create catalog record. If record already exists, it will be replaced.
172+
* @param record pointer to store catalog record
171173
* @param magic record type
172174
* @param name unique record name
173175
* @param id associated shared segment identifier
174176
* @param lock lock the record flag
175177
* @return record index or negative error code
176178
*/
177-
ssize_t publish(uint32_t magic, const LSPString *name, const LSPString *id);
179+
ssize_t publish(Record *record, uint32_t magic, const LSPString *name, const LSPString *id);
180+
181+
/**
182+
* Ensure that record is valid
183+
* @param record record to validate
184+
* @return true if record is valid
185+
*/
186+
bool validate(const Record *record) const;
178187

179188
/**
180189
* Read record from catalog

src/main/shared/Catalog.cpp

+46-3
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ namespace lsp
287287
pHeader->nChanges = changes + 1;
288288
}
289289

290-
ssize_t Catalog::publish(uint32_t magic, const char *name, const char *id)
290+
ssize_t Catalog::publish(Record *record, uint32_t magic, const char *name, const char *id)
291291
{
292292
if (pHeader == NULL)
293293
return -STATUS_CLOSED;
@@ -340,20 +340,25 @@ namespace lsp
340340
str_copy(rec->sId, ID_BYTES, id, id_len);
341341
++rec->nVersion;
342342

343+
// Mark catalog as changed
343344
mark_changed();
344345

346+
// Fill result
347+
if (record != NULL)
348+
fill_record(record, rec);
349+
345350
return index;
346351
}
347352

348-
ssize_t Catalog::publish(uint32_t magic, const LSPString *name, const LSPString *id)
353+
ssize_t Catalog::publish(Record *record, uint32_t magic, const LSPString *name, const LSPString *id)
349354
{
350355
if (pHeader == NULL)
351356
return -STATUS_CLOSED;
352357

353358
if ((name == NULL) || (id == NULL) || (magic == 0))
354359
return -STATUS_BAD_ARGUMENTS;
355360

356-
return publish(magic, name->get_utf8(), id->get_utf8());
361+
return publish(record, magic, name->get_utf8(), id->get_utf8());
357362
}
358363

359364
status_t Catalog::get(Record *record, uint32_t index) const
@@ -389,6 +394,44 @@ namespace lsp
389394
return STATUS_OK;
390395
}
391396

397+
bool Catalog::validate(const Record *record) const
398+
{
399+
if ((record == NULL) || (record->magic == 0))
400+
return false;
401+
if (pHeader == NULL)
402+
return false;
403+
if (record->index >= pHeader->nSize)
404+
return false;
405+
406+
// Lock the mutex
407+
status_t res = hMutex.lock();
408+
if (res != STATUS_OK)
409+
return -res;
410+
lsp_finally {
411+
hMutex.unlock();
412+
};
413+
414+
// Check that record is valid
415+
const sh_record_t *rec = &vRecords[record->index];
416+
if (rec->nMagic != record->magic)
417+
return false;
418+
if (rec->nVersion != record->version)
419+
return false;
420+
421+
LSPString tmp;
422+
if (!tmp.set_utf8(rec->sName, strnlen(rec->sName, NAME_BYTES)))
423+
return false;
424+
if (!record->name.equals(&tmp))
425+
return false;
426+
427+
if (!tmp.set_utf8(rec->sId, strnlen(rec->sId, ID_BYTES)))
428+
return false;
429+
if (!record->name.equals(&tmp))
430+
return false;
431+
432+
return true;
433+
}
434+
392435
status_t Catalog::get(Record *record, const char *name) const
393436
{
394437
if (pHeader == NULL)

src/test/utest/shared/catalog.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ UTEST_BEGIN("dspu.shared", catalog)
4545
UTEST_ASSERT(cat.capacity() == 16);
4646
UTEST_ASSERT(cat.size() == 0);
4747

48-
UTEST_ASSERT(cat.publish(0, "test", "test") < 0);
49-
UTEST_ASSERT(cat.publish(0x11223344, "", "test") < 0);
50-
UTEST_ASSERT(cat.publish(0x11223344, NULL, "test") < 0);
51-
UTEST_ASSERT(cat.publish(0x11223344, "test", "") < 0);
52-
UTEST_ASSERT(cat.publish(0x11223344, "test", NULL) < 0);
53-
UTEST_ASSERT(cat.publish(0x11223344, "test", "test.shm") >= 0);
54-
UTEST_ASSERT(cat.publish(0x11223344, "0123456789012345678901234567890123456789012345678901234567890123456789", "test2.shm") < 0);
55-
UTEST_ASSERT(cat.publish(0x11223344, "test2", "0123456789012345678901234567890123456789012345678901234567890123456789") < 0);
56-
UTEST_ASSERT(cat.publish(0x22334455, "test2", "test2.shm") >= 0);
48+
UTEST_ASSERT(cat.publish(NULL, 0, "test", "test") < 0);
49+
UTEST_ASSERT(cat.publish(NULL, 0x11223344, "", "test") < 0);
50+
UTEST_ASSERT(cat.publish(NULL, 0x11223344, NULL, "test") < 0);
51+
UTEST_ASSERT(cat.publish(NULL, 0x11223344, "test", "") < 0);
52+
UTEST_ASSERT(cat.publish(NULL, 0x11223344, "test", NULL) < 0);
53+
UTEST_ASSERT(cat.publish(NULL, 0x11223344, "test", "test.shm") >= 0);
54+
UTEST_ASSERT(cat.publish(NULL, 0x11223344, "0123456789012345678901234567890123456789012345678901234567890123456789", "test2.shm") < 0);
55+
UTEST_ASSERT(cat.publish(NULL, 0x11223344, "test2", "0123456789012345678901234567890123456789012345678901234567890123456789") < 0);
56+
UTEST_ASSERT(cat.publish(NULL, 0x22334455, "test2", "test2.shm") >= 0);
5757

5858
UTEST_ASSERT(cat.changed());
5959
UTEST_ASSERT(cat.sync());
@@ -86,7 +86,7 @@ UTEST_BEGIN("dspu.shared", catalog)
8686
UTEST_ASSERT(!cat.changed());
8787

8888
// Update record
89-
UTEST_ASSERT(cat.publish(0x33445566, "test", "another-segment.shm") == STATUS_OK);
89+
UTEST_ASSERT(cat.publish(NULL, 0x33445566, "test", "another-segment.shm") == STATUS_OK);
9090
UTEST_ASSERT(cat.changed());
9191
UTEST_ASSERT(cat.sync());
9292
UTEST_ASSERT(!cat.changed());
@@ -145,7 +145,7 @@ UTEST_BEGIN("dspu.shared", catalog)
145145
UTEST_ASSERT(!cat.changed());
146146

147147
// Publish record with long name and description and delete it
148-
UTEST_ASSERT(cat.publish(0x12345678, "abcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnop", "0123456789012345678901234567890123456789012345678901234567890123") >= 0);
148+
UTEST_ASSERT(cat.publish(NULL, 0x12345678, "abcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnop", "0123456789012345678901234567890123456789012345678901234567890123") >= 0);
149149
UTEST_ASSERT(cat.capacity() == 16);
150150
UTEST_ASSERT(cat.size() == 1);
151151
UTEST_ASSERT(cat.changed());

0 commit comments

Comments
 (0)