]> git.ozlabs.org Git - ccan/blob - junkcode/dongre.avinash@gmail.com-clibutils/src/c_util.c
tally: Adapt bucket_range to Samba coding conventions
[ccan] / junkcode / dongre.avinash@gmail.com-clibutils / src / c_util.c
1 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\r
2  *  This file is part of clib library\r
3  *  Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )\r
4  *\r
5  *  Permission is hereby granted, free of charge, to any person obtaining a copy\r
6  *  of this software and associated documentation files (the "Software"), to deal\r
7  *  in the Software without restriction, including without limitation the rights\r
8  *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
9  *  copies of the Software, and to permit persons to whom the Software is\r
10  *  furnished to do so, subject to the following conditions:\r
11  * \r
12  *  The above copyright notice and this permission notice shall be included in\r
13  *  all copies or substantial portions of the Software.\r
14  * \r
15  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
17  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
18  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
19  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
20  *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
21  *  THE SOFTWARE.\r
22  ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/\r
23 \r
24 #include "c_lib.h"\r
25 #include <string.h>\r
26 #include <stdlib.h>\r
27 \r
28 void \r
29 clib_copy( void *destination, void *source, size_t size ) {\r
30     memcpy ( (char*)destination, source, size);\r
31 }\r
32 void\r
33 clib_get ( void *destination, void *source, size_t size) {\r
34     memcpy ( destination, (char*)source, size);\r
35 }\r
36 \r
37 struct clib_object*\r
38 new_clib_object(void *inObject, size_t obj_size) {\r
39     struct clib_object* tmp = (struct clib_object*)malloc(sizeof(struct clib_object));   \r
40     if ( ! tmp )\r
41         return (struct clib_object*)0;\r
42     tmp->size        = obj_size;\r
43     tmp->raw_data    = (void*)malloc(obj_size);\r
44     if ( !tmp->raw_data ) {\r
45         free ( tmp );\r
46         return (struct clib_object*)0;\r
47     }\r
48     memcpy ( tmp->raw_data, inObject, obj_size);\r
49     return tmp;\r
50 }\r
51 \r
52 clib_error\r
53 get_raw_clib_object ( struct clib_object *inObject, void**elem) {\r
54     *elem = (void*)malloc(inObject->size);\r
55     if ( ! *elem )\r
56         return CLIB_ELEMENT_RETURN_ERROR;\r
57     memcpy ( *elem, inObject->raw_data, inObject->size );\r
58 \r
59     return CLIB_ERROR_SUCCESS;\r
60 }\r
61 void \r
62 replace_raw_clib_object(struct clib_object *current_object,void *elem, size_t elem_size) {\r
63         free (current_object->raw_data);\r
64         current_object->raw_data = (void*)malloc(elem_size);\r
65         memcpy ( current_object->raw_data, elem, elem_size);\r
66 }\r
67 \r
68 \r
69 void \r
70 delete_clib_object ( struct clib_object *inObject ) {\r
71     if (inObject) {\r
72         free (inObject->raw_data);\r
73         free (inObject);\r
74     }\r
75 }\r
76 \r
77 char*\r
78 clib_strdup ( char *ptr ) {\r
79     #ifdef WIN32\r
80         return _strdup (ptr);\r
81     #else\r
82         return strdup (ptr);\r
83     #endif\r
84 }\r