Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 19, 2025

Adds iterator-based API to enumerate loaded modules and scan available modules in search paths and packages, using a memory-efficient pattern similar to opendir/readdir/closedir.

Changes

New Types (inc/dmod_types.h):

  • Dmod_ModuleState_t enum: Available, Loaded, Enabled, Running (with Doxygen documentation)
  • Dmod_ModuleInfo_t struct: module name, version, state (with Doxygen documentation)
  • Dmod_ModulesIterator_t: Opaque iterator handle

API (inc/dmod.h, src/system/dmod_system.c):

Dmod_ModulesIterator_t Dmod_OpenModules(void);
const Dmod_ModuleInfo_t* Dmod_ReadModule(Dmod_ModulesIterator_t Iterator);
void Dmod_CloseModules(Dmod_ModulesIterator_t Iterator);

Iterator-based pattern that:

  • Opens an iterator for traversing modules
  • Returns next module on each call (NULL when done)
  • Frees resources when closed
  • Iterates loaded contexts, scans .dmf/.dmfc files in search paths, and queries package manifests
  • Deduplicates entries automatically

Example (examples/system/list_modules/):
Demonstrates listing all modules with formatted output using the iterator API.

Tests (tests/system/public/tests_dmod_read_modules.cpp):
8 tests covering iterator lifecycle, null validation, empty lists, structure integrity, and consistency.

Bug Fixes

Null pointer crash in Dmod_Hlp_AddSearchNode: Added null check for Path parameter before strlen()

Package validation: Check Dmod_Pck_IsSlotUsed() before Dmod_Pck_IsValidSlot() to avoid spurious errors

Missing include: Added <string.h> to dmod_hlp.c

Benefits

  • Memory efficient: No need to allocate large arrays upfront
  • Safer: Pattern similar to familiar POSIX directory functions
  • Flexible: Can iterate as needed, stop early if desired
  • Consistent: Follows established patterns in the codebase

Usage

Dmod_Initialize();

Dmod_ModulesIterator_t iterator = Dmod_OpenModules();
const Dmod_ModuleInfo_t* module;
while((module = Dmod_ReadModule(iterator)) != NULL) {
    printf("%s %s [%s]\n", 
           module->ModuleName,
           module->Version,
           state_to_string(module->State));
}
Dmod_CloseModules(iterator);
Original prompt

This section details on the original issue you should resolve

<issue_title>Dodać możliwość listowania załadowanych i dostępnych modułów</issue_title>
<issue_description>Potrzebujemy API, które pozwoli na wylistowanie modułów załadowanych oraz api które umożliwi listowanie również po modułach dostępnych we wszystkich znanych ścieżkach.

Powinno to być na zasadzie (nazwy możesz dopasować, to tylko propozycje):

typedef struct 
{
     char ModuleName[MAX_MODULE_NAME];
     char Version[MAX_MODULE_VERSION];
     Dmod_ModuleState_t State;
} Dmod_ModuleInfo_t; 

DMOD_BUILTIN_API( Dmod, 1.0, size_t, _ReadModules, ( Dmod_ModuleInfo_t* outModules, size_t Max);

Powinny tam być i moduły załadowane i dostępne w znanych ścieżkach, i dodanych paczkach

</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits November 19, 2025 19:22
Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
Copilot AI changed the title [WIP] Add API for listing loaded and available modules Add API to list loaded and available modules Nov 19, 2025
Copilot AI requested a review from JohnAmadis November 19, 2025 19:36
Copy link
Contributor

@JohnAmadis JohnAmadis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot zmieniamy trochę koncepcje - zrob to podobnie do opendir, readdir i closedir

inc/dmod.h Outdated
DMOD_BUILTIN_API( Dmod, 1.0, bool , _GetPackageInfo, ( uint32_t PackageIndex, char* outName, size_t NameMaxLength, size_t* outSize ) );
DMOD_BUILTIN_API( Dmod, 1.0, uint32_t , _GetMainIndexFromPackage, ( const char* PackageName ) );

DMOD_BUILTIN_API( Dmod, 1.0, size_t , _ReadModules, ( Dmod_ModuleInfo_t* outModules, size_t Max ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tak myślę o tym API i trochę mnie boli, że aż tyle pamięci się marnuje na raz przy tym rozwiązaniu...

Zróbmy może trochę inaczej...

Zrób to podobnie do OpenDir, ReadDir i CloseDir - to będzie lepsze, pewniejsze i bezpieczniejsze

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to iterator pattern (OpenModules/ReadModule/CloseModules) similar to OpenDir/ReadDir/CloseDir. This is much more memory efficient as it doesn't require allocating a large array upfront. Commit: 0601708

Dmod_ModuleType_Count,
} Dmod_ModuleType_t;

typedef enum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jakiś doxygen do tego enuma też by się przydał

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added Doxygen documentation for the enum. Commit: 0601708

char ModuleName[DMOD_MAX_MODULE_NAME_LENGTH];
char Version[DMOD_MAX_VERSION_LENGTH];
Dmod_ModuleState_t State;
} Dmod_ModuleInfo_t;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ta struktura też powinna mieć dokumentację

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added Doxygen documentation for the struct with field descriptions. Commit: 0601708

- Changed from Dmod_ReadModules to OpenModules/ReadModule/CloseModules pattern
- Added Doxygen documentation for Dmod_ModuleState_t enum
- Added Doxygen documentation for Dmod_ModuleInfo_t struct
- Added Dmod_ModulesIterator_t opaque handle type
- Updated example application to use new API
- Updated unit tests (8 tests passing)
- Memory efficient: no large array allocation required

Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 19, 2025

@copilot zmieniamy trochę koncepcje - zrob to podobnie do opendir, readdir i closedir

Refactored to iterator-based API pattern similar to opendir/readdir/closedir:

  • Dmod_OpenModules() - opens iterator
  • Dmod_ReadModule() - gets next module
  • Dmod_CloseModules() - frees resources

This approach is memory efficient and follows familiar POSIX patterns. Commit: 0601708

Copilot AI changed the title Add API to list loaded and available modules Add iterator-based API to list loaded and available modules Nov 19, 2025
Copilot AI requested a review from JohnAmadis November 19, 2025 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dodać możliwość listowania załadowanych i dostępnych modułów

2 participants