Skip to content

Commit

Permalink
Add UserData as struct, clear for storage, memory alloc/free functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Meetem committed Jan 13, 2022
1 parent 70538c7 commit de16207
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 3 deletions.
7 changes: 7 additions & 0 deletions BladeStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ typedef struct UsbHandle_s{
void *handle;
}UsbHandle;

typedef struct UserData_s
{
void *ptr;
int32_t length;
int32_t autoFree;
}UserData;

typedef struct KeyboardRow_s
{
uint8_t rowid; // Row ID (0-5) used by the EC to update each row
Expand Down
11 changes: 11 additions & 0 deletions DescriptionStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,15 @@ namespace librazerblade {
this->put(d);
}
}

void DescriptionStorage::clear()
{
for(auto &d : descriptions){
if(d.userData.ptr != nullptr && d.userData.autoFree > 0){
free(d.userData.ptr);
}
}

descriptions.clear();
}
}
1 change: 1 addition & 0 deletions DescriptionStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace librazerblade {
void put(LaptopDescription description);
LaptopDescription *getAll(int32_t *size);
void set(int32_t idx, LaptopDescription description);
void clear();

void initializeWithDefault();
protected:
Expand Down
2 changes: 1 addition & 1 deletion LaptopDescription.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ typedef struct LaptopDescription_s
char name[256];
LaptopFan fan;
BladeCapabilities capabilities;
void* userData;
UserData userData;
} LaptopDescription;

typedef struct LaptopState_s
Expand Down
Binary file modified build/msvc/bin/librazerblade.dll
Binary file not shown.
7 changes: 7 additions & 0 deletions build/msvc/include/BladeStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ typedef struct UsbHandle_s{
void *handle;
}UsbHandle;

typedef struct UserData_s
{
void *ptr;
int32_t length;
int32_t autoFree;
}UserData;

typedef struct KeyboardRow_s
{
uint8_t rowid; // Row ID (0-5) used by the EC to update each row
Expand Down
2 changes: 1 addition & 1 deletion build/msvc/include/LaptopDescription.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ typedef struct LaptopDescription_s
char name[256];
LaptopFan fan;
BladeCapabilities capabilities;
void* userData;
UserData userData;
} LaptopDescription;

typedef struct LaptopState_s
Expand Down
6 changes: 6 additions & 0 deletions build/msvc/include/librazerblade.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ CallType librazerblade_DescriptionStorage_get(uint16_t vendor, uint16_t product,
DllExport void CallType librazerblade_DescriptionStorage_put(LaptopDescription description);
DllExport LaptopDescription* CallType librazerblade_DescriptionStorage_getAll(int32_t* size);
DllExport void CallType librazerblade_DescriptionStorage_set(int32_t idx, LaptopDescription description);
DllExport void CallType librazerblade_DescriptionStorage_clear();
//#endregion

//#region Memory
DllExport void* CallType librazerblade_malloc(int32_t size);
DllExport UserData CallType librazerblade_UserData_fromMemory(void *ptr, int32_t size, int32_t autoFree);
DllExport void CallType librazerblade_free(void *ptr);
//#endregion
#ifdef __cplusplus
}
#endif
Expand Down
Binary file modified build/msvc/lib/librazerblade.lib
Binary file not shown.
37 changes: 36 additions & 1 deletion librazerblade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ DllExport void CallType librazerblade_deinitialize()
#ifdef LIBUSB_DEFAULT_IMPLEMENTATION
closeLibUsb();
#endif

librazerblade::gStorage.clear();
}


Expand Down Expand Up @@ -311,7 +313,8 @@ DllExport KeyboardRow CallType librazerblade_PacketUtil_getRow(RazerPacket* pkt)
//#endregion

//#region Laptop
DllExport LaptopPtr CallType librazerblade_Laptop_new(LaptopDescription description, UsbHandle usbHandle, UsbDevice device)
DllExport LaptopPtr
CallType librazerblade_Laptop_new(LaptopDescription description, UsbHandle usbHandle, UsbDevice device)
{
return new Laptop(description, usbHandle, device);
}
Expand Down Expand Up @@ -531,6 +534,38 @@ DllExport void CallType librazerblade_DescriptionStorage_set(int32_t idx, Laptop
{
return gStorage.set(idx, description);
}
DllExport void CallType librazerblade_DescriptionStorage_clear()
{
return gStorage.clear();
}
//#endregion

//#region Memory
DllExport void* CallType librazerblade_malloc(int32_t size)
{
return malloc(size);
}

DllExport UserData CallType librazerblade_UserData_fromMemory(void* ptr, int32_t size, int32_t autoFree)
{
if (ptr == nullptr) {
return UserData{nullptr, 0, 0};
}

auto mem = malloc(size);
if (mem == nullptr) {
return UserData{nullptr, 0, 0};
}

memcpy(mem, ptr, size);
return UserData{mem, size, autoFree};
}

DllExport void CallType librazerblade_free(void* ptr)
{
free(ptr);
}
//#endregion

}

6 changes: 6 additions & 0 deletions librazerblade.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ CallType librazerblade_DescriptionStorage_get(uint16_t vendor, uint16_t product,
DllExport void CallType librazerblade_DescriptionStorage_put(LaptopDescription description);
DllExport LaptopDescription* CallType librazerblade_DescriptionStorage_getAll(int32_t* size);
DllExport void CallType librazerblade_DescriptionStorage_set(int32_t idx, LaptopDescription description);
DllExport void CallType librazerblade_DescriptionStorage_clear();
//#endregion

//#region Memory
DllExport void* CallType librazerblade_malloc(int32_t size);
DllExport UserData CallType librazerblade_UserData_fromMemory(void *ptr, int32_t size, int32_t autoFree);
DllExport void CallType librazerblade_free(void *ptr);
//#endregion
#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit de16207

Please sign in to comment.