Tizen 2.1 base
[framework/multimedia/libmm-common.git] / mm_list_private.c
1 /*
2  * libmm-common
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jonghyuk Choi <jhchoi.choi@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <mm_error.h>
26
27 #include <mm_debug.h>
28 #include <mm_list_private.h>
29
30 /**
31  * Definition of list item
32  */
33 typedef struct {
34         int                     id;
35         void            *data;
36         void            *next;
37 } mmf_list_t;
38
39 /**
40  * Globals
41  */
42 static mmf_list_t       *g_list = NULL;
43 static int                      g_list_id = 0;
44
45 EXPORT_API
46 int mmf_list_append(void *data)
47 {
48         mmf_list_t *item;
49         mmf_list_t *list;
50
51         item = (mmf_list_t *) malloc(sizeof(mmf_list_t));
52
53         if (item == NULL) {
54                 debug_error("memory allocation error\n");
55                 return MM_ERROR_OUT_OF_MEMORY;
56         }
57
58         /* set new list information */
59         memset(item, 0, sizeof(mmf_list_t));
60
61         item->id = g_list_id++;
62         item->data = data;
63         list = g_list;
64
65         if (list == NULL) {
66                 /* first item */
67                 g_list = item;
68         }
69         else {
70                 list = g_list;
71
72                 /* move to last */
73                 while (list->next) {
74                         list = (mmf_list_t *) list->next;
75                 }
76                 list->next = item;
77         }
78
79         return item->id;
80 }
81
82 EXPORT_API
83 int mmf_list_remove(int handle)
84 {
85         mmf_list_t *list;
86         mmf_list_t *prev;
87
88         list = g_list;
89         prev = NULL;
90
91         while (list) {
92                 if (list->id == handle) {
93                         /* remove handle */
94                         if (prev) {
95                                 prev->next = list->next;
96                         }
97                         else {
98                                 g_list = list->next;
99                         }
100
101                         free(list);
102
103                         return MM_ERROR_NONE;
104                 }
105
106                 prev = list;
107                 list = (mmf_list_t *) list->next;
108         }
109
110         debug_error("handle not found\n");
111
112         return MM_ERROR_INVALID_ARGUMENT;
113 }
114
115 EXPORT_API
116 void *mmf_list_find(int handle)
117 {
118         mmf_list_t *list;
119
120         list = g_list;
121
122         while (list) {
123                 if (list->id == handle) {
124                         return list->data;
125                 }
126
127                 list = (mmf_list_t *) list->next;
128         }
129
130         debug_error("handle not found\n");
131
132         return NULL;
133 }
134
135 EXPORT_API
136 int mmf_list_find_by_data(void *data)
137 {
138         mmf_list_t *list;
139
140         list = g_list;
141
142         while (list) {
143                 if (list->data == data) {
144                         return list->id;
145                 }
146
147                 list = (mmf_list_t *) list->next;
148         }
149
150         debug_error("handle not found\n");
151
152         return -1;
153 }
154