pepper: utils: Add 32bit integer ID allocator 11/56911/1
authorTaekyun Kim <tkq.kim@samsung.com>
Wed, 13 Jan 2016 11:04:00 +0000 (20:04 +0900)
committerTaekyun Kim <tkq.kim@samsung.com>
Wed, 13 Jan 2016 12:20:45 +0000 (21:20 +0900)
Change-Id: I25d391b8f5eda12e01f8e7de31c82a86f0c71d53

src/lib/pepper/Makefile.am
src/lib/pepper/pepper-utils.h
src/lib/pepper/utils.c [new file with mode: 0644]

index 58778f2cbfc543340c26de8495f5b59d3550620a..94a8df43783d34b14231662cdd387795a71a89d7 100644 (file)
@@ -22,6 +22,7 @@ libpepper_la_SOURCES = pepper.h                 \
                        buffer.c                 \
                        view.c                   \
                        plane.c                  \
+                       utils.c                  \
                        utils-file.c             \
                        utils-map.c              \
                        utils-log.c              \
index 98da3f501caf08b3ff73ff59fe51ccaecd998869..23e7e38b698e34127de3db9a5be106e625ad5c55 100644 (file)
@@ -277,6 +277,28 @@ pepper_map_get(pepper_map_t *map, const void *key);
 PEPPER_API void
 pepper_map_set(pepper_map_t *map, const void *key, void *data, pepper_free_func_t free_func);
 
+typedef struct pepper_id_allocator  pepper_id_allocator_t;
+
+struct pepper_id_allocator
+{
+    uint32_t        next_id;
+    int             free_id_count;
+    int             free_id_size;
+    uint32_t       *free_ids;
+};
+
+PEPPER_API void
+pepper_id_allocator_init(pepper_id_allocator_t *allocator);
+
+PEPPER_API void
+pepper_id_allocator_fini(pepper_id_allocator_t *allocator);
+
+PEPPER_API uint32_t
+pepper_id_allocator_alloc(pepper_id_allocator_t *allocator);
+
+PEPPER_API void
+pepper_id_allocator_free(pepper_id_allocator_t *allocator, uint32_t id);
+
 PEPPER_API int
 pepper_create_anonymous_file(off_t size);
 
diff --git a/src/lib/pepper/utils.c b/src/lib/pepper/utils.c
new file mode 100644 (file)
index 0000000..a4c852e
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+* Copyright © 2008-2012 Kristian Høgsberg
+* Copyright © 2010-2012 Intel Corporation
+* Copyright © 2011 Benjamin Franzke
+* Copyright © 2012 Collabora, Ltd.
+* Copyright © 2015 S-Core Corporation
+* Copyright © 2015-2016 Samsung Electronics co., Ltd. All Rights Reserved.
+*
+* Permission is hereby granted, free of charge, to any person obtaining a
+* copy of this software and associated documentation files (the "Software"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice (including the next
+* paragraph) shall be included in all copies or substantial portions of the
+* Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+* DEALINGS IN THE SOFTWARE.
+*/
+
+#include "pepper-utils.h"
+
+PEPPER_API void
+pepper_id_allocator_init(pepper_id_allocator_t *allocator)
+{
+    memset(allocator, 0x00, sizeof(pepper_id_allocator_t));
+    allocator->next_id = 1;
+}
+
+PEPPER_API void
+pepper_id_allocator_fini(pepper_id_allocator_t *allocator)
+{
+    if (allocator->free_ids)
+        free(allocator->free_ids);
+}
+
+PEPPER_API uint32_t
+pepper_id_allocator_alloc(pepper_id_allocator_t *allocator)
+{
+    if (allocator->free_id_count)
+        return allocator->free_ids[--allocator->free_id_count];
+
+    return allocator->next_id++;
+}
+
+PEPPER_API void
+pepper_id_allocator_free(pepper_id_allocator_t *allocator, uint32_t id)
+{
+    if (allocator->free_id_size <= allocator->free_id_count)
+    {
+        allocator->free_id_size += 64;
+        allocator->free_ids = realloc(allocator->free_ids, allocator->free_id_size);
+    }
+
+    allocator->free_ids[allocator->free_id_count++] = id;
+}