Skip to content

Commit c91b1f8

Browse files
author
Robin Müller
committed
Converted the int_array to a generic array that can be used also for other types
1 parent 894974d commit c91b1f8

File tree

5 files changed

+88
-18
lines changed

5 files changed

+88
-18
lines changed

src/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
bin_PROGRAMS = touch_gestures
2-
touch_gestures_SOURCES = main.c int_array.c gestures_device.c gesture_detection.c configuraion.c
2+
touch_gestures_SOURCES = main.c array.c gestures_device.c gesture_detection.c configuraion.c

src/int_array.c renamed to src/array.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@
2424

2525
#include <stdlib.h>
2626

27-
#include "int_array.h"
27+
#include "array.h"
2828

29-
int_array_t *new_int_array(size_t length) {
30-
int_array_t *array;
31-
/* we're allocating the size of basic t_int_array
32-
(which already contains space for one int)
33-
and additional space for length-1 ints */
34-
array = malloc(sizeof(int_array_t) + sizeof(int) * (length - 1));
29+
void *new_array(size_t length, size_t struct_size, size_t type_size) {
30+
void *array;
31+
/* we're allocating the size of basic array struct
32+
(which already contains space for one type)
33+
and additional space for length-1 type */
34+
array = malloc(struct_size + type_size * (length - 1));
3535
if(!array) {
3636
return 0;
3737
}
38-
array->length = length;
38+
// the length is locatated at the beginning of the stuct
39+
*(size_t*) array = length;
3940
return array;
4041
}

src/array.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2014 Robin Müller.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#ifndef ARRAY_H_
26+
#define ARRAY_H_
27+
28+
#include <stddef.h>
29+
30+
void *new_array(size_t length, size_t struct_size, size_t type_size);
31+
32+
#endif // ARRAY_H_

src/input_event_array.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2014 Robin Müller.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#ifndef INPUT_EVENT_ARRAY_H_
26+
#define INPUT_EVENT_ARRAY_H_
27+
28+
#include <linux/input.h>
29+
30+
#include "array.h"
31+
32+
typedef struct input_event_array {
33+
size_t length;
34+
struct input_event data[1];
35+
} input_event_array_t;
36+
37+
#define new_input_event_array(length) (input_event_array_t*) new_array(length, sizeof(input_event_array_t), sizeof(struct input_event))
38+
39+
#endif // INPUT_EVENT_ARRAY_H_

src/int_array.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@
2222
* THE SOFTWARE.
2323
*/
2424

25-
#ifndef INTEGER_ARRAY_H_
26-
#define INTEGER_ARRAY_H_
25+
#ifndef INT_ARRAY_H_
26+
#define INT_ARRAY_H_
2727

28-
#include <stddef.h>
28+
#include "array.h"
2929

30-
struct int_array {
30+
typedef struct int_array {
3131
size_t length;
3232
int data[1];
33-
};
33+
} int_array_t;
3434

35-
typedef struct int_array int_array_t;
35+
#define new_int_array(length) (int_array_t*) new_array(length, sizeof(int_array_t), sizeof(int))
3636

37-
int_array_t * new_int_array(size_t length);
38-
39-
#endif // INTEGER_ARRAY_H_
37+
#endif // INT_ARRAY_H_

0 commit comments

Comments
 (0)