1 /*
2 * Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "nsk_list.h"
28 #include "nsk_tools.h"
29
30 extern "C" {
31
32 #define NSK_LIST_INIT_COUNT 20
33
34 typedef struct nsk_list_infoStruct {
35 const void **arr;
36 int elements_count;
37 int allocated_count;
38 } nsk_list_info;
39
40
41 static int nsk_list_size_void = sizeof(void *);
42
43 /* ============================================================================= */
44
nsk_list_create()45 const void* nsk_list_create() {
46
47 nsk_list_info *list_info;
48
49 /* create nsk_list_info */
50 list_info = (nsk_list_info *)malloc(sizeof(nsk_list_info));
51 if (list_info == NULL) {
52 return NULL;
53 }
54
55 list_info->allocated_count = NSK_LIST_INIT_COUNT;
56 list_info->elements_count = 0;
57 list_info->arr = (const void **)malloc(list_info->allocated_count * nsk_list_size_void);
58 if (list_info->arr == NULL) {
59 free(list_info);
60 return NULL;
61 }
62
63 return list_info;
64 }
65
66 /* ============================================================================= */
67
nsk_list_destroy(const void * plist)68 int nsk_list_destroy(const void *plist) {
69
70 const nsk_list_info *list_info = (const nsk_list_info *)plist;
71
72 free((void *)list_info->arr);
73 free((void *)plist);
74
75 return NSK_TRUE;
76 }
77
78 /* ============================================================================= */
79
nsk_list_add(const void * plist,const void * p)80 int nsk_list_add(const void *plist, const void *p) {
81
82 nsk_list_info *list_info = (nsk_list_info *)plist;
83
84 if (list_info->elements_count >= list_info->allocated_count) {
85 list_info->allocated_count += NSK_LIST_INIT_COUNT;
86 list_info->arr = (const void **)realloc((void *)list_info->arr, list_info->allocated_count * nsk_list_size_void);
87 if (list_info->arr == NULL) {
88 return NSK_FALSE;
89 }
90 }
91 list_info->arr[list_info->elements_count++] = p;
92
93 return NSK_TRUE;
94 }
95
96 /* ============================================================================= */
97
nsk_list_remove(const void * plist,int ind)98 int nsk_list_remove(const void *plist, int ind) {
99
100 nsk_list_info *list_info = (nsk_list_info *)plist;
101
102 if ((list_info->elements_count <= 0)
103 || ((ind < 0) || (ind >= list_info->elements_count)))
104 return NSK_FALSE;
105
106 {
107 int i;
108 for (i = ind+1; i < list_info->elements_count; i++) {
109 list_info->arr[i - 1] = list_info->arr[i];
110 }
111 }
112 list_info->arr[--list_info->elements_count] = 0;
113
114 return NSK_TRUE;
115 }
116
117 /* ============================================================================= */
118
nsk_list_getCount(const void * plist)119 int nsk_list_getCount(const void *plist) {
120
121 return ((const nsk_list_info *)plist)->elements_count;
122 }
123
124 /* ============================================================================= */
125
nsk_list_get(const void * plist,int i)126 const void* nsk_list_get(const void *plist, int i) {
127
128 const nsk_list_info *list_info = (const nsk_list_info *)plist;
129
130 if ((i >= 0) && (i < list_info->elements_count)) {
131 return list_info->arr[i];
132 }
133
134 return NULL;
135 }
136
137 }
138
139 /* ============================================================================= */
140