Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More array abstraction use in ptp2 #1040

Merged
merged 19 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
b75b485
ptp2: use new array abstraction for PTPParams->eos_events
axxel Oct 12, 2024
1ee7a0f
ptp2: make a failed realloc in array_extend() return GP_ERROR_NO_MEMORY
axxel Oct 12, 2024
b8ab81a
ptp2: free_propval naming cleanup, dead code removal and fixed 2 mem-…
axxel Oct 12, 2024
7572b0a
ptp2: remove code duplication in parse_9301_value
axxel Oct 12, 2024
a403232
ptp2: add #include "ptp.h" in ptp-pack.c to help IDE find the symbols
axxel Oct 12, 2024
76ad94b
ptp2: move timestamp into PTPDevicePropDesc, remove PTPDeviceProperty
axxel Oct 12, 2024
57d6658
ptp2: replace PTPCanon_Property with single member PTPDevicePropDesc
axxel Oct 12, 2024
9b13dd7
ptp2: another round of memory leak fixes of PTPCanonEOSEvent
axxel Oct 13, 2024
218ce86
ptp2: introduce array_free_recursive(array, destructor) macro
axxel Oct 13, 2024
d1bd7a2
ptp2: use ARRAY abstraction for PTPParams::canon_props
axxel Oct 13, 2024
96af051
ptp2: reduce sizeof(PTPDevicePropDesc) by 4 bytes via member reordering
axxel Oct 13, 2024
fef9bf1
ptp2: use array abstraction for dpd_cache
axxel Oct 13, 2024
459180b
ptp2: use array abstraction for params->events
axxel Oct 13, 2024
10c1652
ptp2: ptp_object_want readability improvements and dead code removal
axxel Oct 14, 2024
abf1543
ptp2: fix bug in array_extend usage
axxel Oct 14, 2024
b02f986
ptp2: use array abstraction for params->objects
axxel Oct 14, 2024
b2755d7
ptp2: move array abstraction macros to new array.h for potential
axxel Oct 14, 2024
7c29127
ptp2: updated copyright info
axxel Oct 14, 2024
f131514
ptp2: fix ci build failure by adding new array.h to Makefile-files
axxel Oct 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions camlibs/ptp2/Makefile-files
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ptp2_la_SOURCES += %reldir%/olympus-wrap.h
ptp2_la_SOURCES += %reldir%/chdk.c
ptp2_la_SOURCES += %reldir%/fujiptpip.c
ptp2_la_SOURCES += %reldir%/ptpip-private.h
ptp2_la_SOURCES += %reldir%/array.h

ptp2_la_CFLAGS = $(camlib_cflags)
ptp2_la_CPPFLAGS = $(camlib_cppflags)
Expand Down
121 changes: 121 additions & 0 deletions camlibs/ptp2/array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* array.h
*
* Copyright (C) 2024 Axel Waggershauser <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/

#ifndef CAMLIBS_PTP2_ARRAY_H
#define CAMLIBS_PTP2_ARRAY_H

#define ARRAYSIZE(ARRAY) (sizeof(ARRAY) / sizeof(ARRAY[0]))

#define move(dst, src) do { \
dst = src; \
memset(&(src), 0, sizeof(src)); \
} while(0)

/* The follow set of macros implements a generic array or list of TYPE.
* This is basically a TYPE* pointer and a length integer. This structure
* together with the typical use-cases repeats regularly throughout the
* codebase. It raises the level of abstraction and improves code
* readabilty. axxel is a c++ developer and misses his STL ;)
*
* A typical life-cycle lookes like this:
*
* typedef ARRAY_OF(PTPObject) PTPObjects;
* PTPObjects objects;
* array_push_back(&objects, some_value);
* PTPObject *o;
* array_push_back_empty(&objects, &o);
* o->some_prop = 1;
* for_each(PTPObject*, pobj, objects)
* pobj->call_some_func();
* free_array_recursive(&objects, ptp_free_object);
*/

#define ARRAY_OF(TYPE) struct ArrayOf##TYPE \
{ \
TYPE *val; \
uint32_t len; \
}

/* TODO: with support for C23, we can improve the for_each macro by dropping the TYPE argument
* #define for_each(PTR, ARRAY) for (typeof(ARRAY.val) PTR = ARRAY.val; PTR != ARRAY.val + ARRAY.len; ++PTR)
*/
#define for_each(TYPE, PTR, ARRAY) \
for (TYPE PTR = ARRAY.val; PTR != ARRAY.val + ARRAY.len; ++PTR)

#define free_array(ARRAY) do { \
free ((ARRAY)->val); \
(ARRAY)->val = 0; \
(ARRAY)->len = 0; \
} while (0)

#define free_array_recusive(ARRAY, DESTRUCTOR) do { \
for (uint32_t _i = 0; _i < (ARRAY)->len; ++_i) \
DESTRUCTOR (&(ARRAY)->val[_i]); \
free_array (ARRAY); \
} while (0)

#define array_extend_capacity(ARRAY, LEN) do { \
(ARRAY)->val = realloc((ARRAY)->val, ((ARRAY)->len + (LEN)) * sizeof((ARRAY)->val[0])); \
if (!(ARRAY)->val) { \
GP_LOG_E ("Out of memory: 'realloc' of %ld bytes failed.", ((ARRAY)->len + (LEN)) * sizeof((ARRAY)->val[0])); \
return GP_ERROR_NO_MEMORY; \
} \
memset((ARRAY)->val + (ARRAY)->len, 0, LEN * sizeof((ARRAY)->val[0])); \
} while(0)

#define array_push_back_empty(ARRAY, PITER) do { \
array_extend_capacity(ARRAY, 1); \
*PITER = &(ARRAY)->val[(ARRAY)->len++]; \
} while(0)

#define array_push_back(ARRAY, VAL) do { \
array_extend_capacity(ARRAY, 1); \
move((ARRAY)->val[(ARRAY)->len++], VAL); \
} while(0)

#define array_append_copy(DST, SRC) do { \
array_extend_capacity(DST, (SRC)->len); \
memcpy((DST)->val + (DST)->len, (SRC)->val, (SRC)->len * sizeof((SRC)->val[0])); \
(DST)->len += (SRC)->len; \
} while(0)

#define array_append(DST, SRC) do { \
if ((DST)->len) { \
array_append_copy(DST, SRC); \
free_array (SRC); \
} else { \
free_array (DST); \
*(DST) = *(SRC); \
} \
} while(0)

#define array_remove(ARRAY, ITER) do { \
if (ITER < (ARRAY)->val || ITER >= (ARRAY)->val + (ARRAY)->len) \
break; \
memmove (ITER, ITER + 1, ((ARRAY)->len - (ITER + 1 - (ARRAY)->val)) * sizeof((ARRAY)->val[0])); \
(ARRAY)->len--; \
} while(0)

#define array_pop_front(ARRAY, VAL) do { \
*VAL = (ARRAY)->val[0]; \
array_remove(ARRAY, (ARRAY)->val); \
} while(0)

#endif
6 changes: 3 additions & 3 deletions camlibs/ptp2/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -12115,7 +12115,7 @@ _set_config (Camera *camera, const char *confname, CameraWidget *window, GPConte
ret = translate_ptp_result (ret_ptp);
}
}
ptp_free_devicepropvalue (cursub->type, &propval);
ptp_free_propvalue (cursub->type, &propval);
}
ptp_free_devicepropdesc(&dpd);
if (ret != GP_OK) continue; /* see if we have another match */
Expand Down Expand Up @@ -12146,7 +12146,7 @@ _set_config (Camera *camera, const char *confname, CameraWidget *window, GPConte
ret = translate_ptp_result (ret_ptp);
}
}
ptp_free_devicepropvalue(cursub->type, &propval);
ptp_free_propvalue(cursub->type, &propval);
} else
gp_context_error (context, _("Parsing the value of widget '%s' / 0x%04x failed with %d."), _(cursub->label), cursub->propid, ret);
ptp_free_devicepropdesc(&dpd);
Expand Down Expand Up @@ -12266,7 +12266,7 @@ _set_config (Camera *camera, const char *confname, CameraWidget *window, GPConte
_(label), propid, ret, _(ptp_strerror(ret, params->deviceinfo.VendorExtensionID)));
ret = GP_ERROR;
}
ptp_free_devicepropvalue (dpd.DataType, &propval);
ptp_free_propvalue (dpd.DataType, &propval);
ptp_free_devicepropdesc (&dpd);
if (mode == MODE_SINGLE_SET)
return GP_OK;
Expand Down
Loading
Loading