00001 /* 00002 * Copyright 2008, 2009 Alexandros Frantzis, Michael Iatrou 00003 * 00004 * This file is part of libbls. 00005 * 00006 * libbls is free software: you can redistribute it and/or modify it under the 00007 * terms of the GNU Lesser General Public License as published by the Free Software 00008 * Foundation, either version 3 of the License, or (at your option) any later 00009 * version. 00010 * 00011 * libbls is distributed in the hope that it will be useful, but WITHOUT ANY 00012 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00013 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 00014 * details. 00015 * 00016 * You should have received a copy of the GNU General Public License along with 00017 * libbls. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 /** 00021 * @file buffer_source.c 00022 * 00023 * Buffer source implementation 00024 */ 00025 00026 #include "buffer_source.h" 00027 #include "data_object.h" 00028 #include "data_object_memory.h" 00029 #include "data_object_file.h" 00030 #include "debug.h" 00031 00032 00033 #include <errno.h> 00034 #include <stdlib.h> 00035 00036 #pragma GCC visibility push(default) 00037 00038 /** 00039 * Creates a memory source for bless_buffer_t. 00040 * 00041 * If the mem_free function is NULL the data won't be freed 00042 * when this source object is freed. 00043 * 00044 * @param[out] src the created bless_buffer_source_t. 00045 * @param data the data related to the source 00046 * @param length the length of the data 00047 * @param mem_free the function to call to free the data 00048 * 00049 * @return the operation error code 00050 */ 00051 int bless_buffer_source_memory(bless_buffer_source_t **src, void *data, 00052 size_t length, bless_mem_free_func *mem_free) 00053 { 00054 if (src == NULL || data == NULL) 00055 return_error(EINVAL); 00056 00057 /* Create the data object and set its data free function */ 00058 data_object_t *obj; 00059 int err = data_object_memory_new(&obj, data, length); 00060 if (err) 00061 return_error(err); 00062 00063 err = data_object_memory_set_free_func(obj, mem_free); 00064 if (err) { 00065 data_object_free(obj); 00066 return_error(err); 00067 } 00068 00069 /* 00070 * Increase the usage count of this data object. This is done 00071 * so that the user is able to use it safely multiple times 00072 * eg appending various parts of it to the buffer. When the 00073 * user is done they should call bless_buffer_source_unref(). 00074 */ 00075 err = data_object_update_usage(obj, 1); 00076 if (err) { 00077 data_object_free(obj); 00078 return_error(err); 00079 } 00080 00081 *src = obj; 00082 00083 return 0; 00084 } 00085 00086 /** 00087 * Creates a file source for bless_buffer_t. 00088 * 00089 * If the data_free function is NULL the file won't be closed 00090 * when this source object is freed. 00091 * 00092 * @param[out] src the created bless_buffer_source_t. 00093 * @param fd the file descriptor associated with this source object 00094 * @param file_close the function to call to close the file 00095 * 00096 * @return the operation error code 00097 */ 00098 int bless_buffer_source_file(bless_buffer_source_t **src, int fd, 00099 bless_file_close_func *file_close) 00100 { 00101 if (src == NULL) 00102 return_error(EINVAL); 00103 00104 /* Create the data object and set its data free function */ 00105 data_object_t *obj; 00106 int err = data_object_file_new(&obj, fd); 00107 if (err) 00108 return_error(err); 00109 00110 err = data_object_file_set_close_func(obj, file_close); 00111 if (err) { 00112 data_object_free(obj); 00113 return_error(err); 00114 } 00115 00116 /* 00117 * Increase the usage count of this data object. This is done 00118 * so that the user is able to use it safely multiple times 00119 * eg appending various parts of it to the buffer. When the 00120 * user is done they should call bless_buffer_source_unref(). 00121 */ 00122 err = data_object_update_usage(obj, 1); 00123 if (err) { 00124 data_object_free(obj); 00125 return_error(err); 00126 } 00127 00128 *src = obj; 00129 00130 return 0; 00131 } 00132 00133 /** 00134 * Decreases the usage count of a source object. 00135 * 00136 * This function should be called when the user is done 00137 * using a source object. Failing to do so will lead to 00138 * a memory leak. 00139 * 00140 * @param src the source object to decrease the usage count of 00141 * 00142 * @return the operation error code 00143 */ 00144 int bless_buffer_source_unref(bless_buffer_source_t *src) 00145 { 00146 if (src == NULL) 00147 return_error(EINVAL); 00148 00149 data_object_t *obj = (data_object_t *) src; 00150 00151 int err = data_object_update_usage(obj, -1); 00152 if (err) 00153 return_error(err); 00154 00155 return 0; 00156 } 00157 00158 #pragma GCC visibility pop 00159