framework_Container.c 8.32 KB
Newer Older
Gus Grubba's avatar
Gus Grubba committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
/**************************************************************************
 * Copyright (C) 2015 Eff'Innov Technologies 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Developped by Eff'Innov Technologies : contact@effinnov.com
 * 
 **************************************************************************/

#include "TML/inc/framework_Container.h"
#include <stdlib.h>
#include <string.h>

#define MEM_ALLOCATOR(x) malloc(x)
#define MEM_DEALLOCATOR(x) free(x)

#define START_OK "valid_start"
#define START_KO "deleted_start"

#define END_OK "valid_end"
#define END_KO "deleted_end"

#define STR_CMP(x, y, z) memcmp(x, y, z)
#define STR_CPY(x, y, z) memcpy(x, y, z)
 
 typedef struct Container
 {
    char         start[14];
    void       **m_data;
    uint32_t     m_size_data;
    uint32_t     m_alloc_size;
    char         end[12];
 }Container_h;
 
void container_check_size(Container_h* pContainer);
void container_rebuild(Container_h* pContainer, uint32_t index);
CONTAINER_STATUS container_isValid(Container_h* pContainer);


CONTAINER_STATUS container_create(void** pContainer, uint32_t size)
{
    CONTAINER_STATUS lStatus = CONTAINER_SUCCESS;
    Container_h *lContainer = NULL;
    
    if (NULL == pContainer)
    {
        lStatus = CONTAINER_INVALID_PARAM;
    }
    
    if(CONTAINER_SUCCESS == lStatus)
    {
        *pContainer = MEM_ALLOCATOR(sizeof(Container_h));
        if (NULL == *pContainer)
        {
            lStatus = CONTAINER_FAILED;
        }
        else
        {
            lContainer = (Container_h*)*pContainer;
            lContainer->m_data=(void**)MEM_ALLOCATOR(size*sizeof(void*));
            memset(lContainer->m_data,0,size*sizeof(void*));
            lContainer->m_size_data=0;
            lContainer->m_alloc_size=size;
            STR_CPY(lContainer->start, START_OK, sizeof(START_OK));
            STR_CPY(lContainer->end, END_OK, sizeof(END_OK));
        }
    }
    
    return lStatus;
}

CONTAINER_STATUS container_delete(void* pContainer)
{
    CONTAINER_STATUS lStatus = CONTAINER_SUCCESS;
    Container_h *lContainer = (Container_h *) pContainer;
    
    lStatus = container_isValid(lContainer);
    
    if(CONTAINER_SUCCESS == lStatus)
    {
        if(NULL != lContainer->m_data)
        {
            MEM_DEALLOCATOR(lContainer->m_data);
            lContainer->m_data = NULL;
            
            STR_CPY(lContainer->start, START_KO, sizeof(START_OK));
            STR_CPY(lContainer->end, END_KO, sizeof(END_OK));
        }
    }
    
    return lStatus;
}


CONTAINER_STATUS container_add(void* pContainer, void* _ptr)
{
    CONTAINER_STATUS lStatus = CONTAINER_SUCCESS;
    Container_h *lContainer = (Container_h *) pContainer;
    
    lStatus = container_isValid(lContainer);
    
    if(CONTAINER_SUCCESS == lStatus)
    {
        container_check_size(lContainer);
        lContainer->m_data[lContainer->m_size_data] = _ptr;
        lContainer->m_size_data++;
    }
    
    return lStatus;
}


CONTAINER_STATUS container_set(void* pContainer, uint32_t index,void*_ptr, void** _old)
{
    CONTAINER_STATUS lStatus = CONTAINER_SUCCESS;
    Container_h *lContainer = (Container_h *) pContainer;
    
    lStatus = container_isValid(lContainer);
    
    
    *_old = NULL;
    if(CONTAINER_SUCCESS == lStatus)
    {
        if (index > lContainer->m_size_data)
        {
            container_add(pContainer, _ptr);
        }
        else
        {
            *_old = lContainer->m_data[index];
            lContainer->m_data[index] = _ptr;
        }
    }
    
    return lStatus;
}

CONTAINER_STATUS container_remove(void* pContainer, uint32_t index, void** _old)
{
    CONTAINER_STATUS lStatus = CONTAINER_SUCCESS;
    Container_h *lContainer = (Container_h *) pContainer;
    
    lStatus = container_isValid(lContainer);
    
    *_old = NULL;
    if(CONTAINER_SUCCESS == lStatus)
    {
        if (index < lContainer->m_size_data)
        {
            *_old = lContainer->m_data[index];
            lContainer->m_data[index] = NULL;
            
            // this is not the last one
            if (index != (lContainer->m_size_data - 1))
            {
                container_rebuild(lContainer, index);
            }
            lContainer->m_size_data--;
        }
    }
    return lStatus;
}

CONTAINER_STATUS container_get(void* pContainer, uint32_t index, void** _out)
{
    CONTAINER_STATUS lStatus = CONTAINER_SUCCESS;
    Container_h *lContainer = (Container_h *) pContainer;
    
    lStatus = container_isValid(lContainer);
    
    *_out = NULL;
    if(CONTAINER_SUCCESS == lStatus)
    {
        if (index < lContainer->m_size_data)
        {
            *_out = lContainer->m_data[index];
        }
    }
    
    return lStatus;
}

CONTAINER_STATUS container_size(void* pContainer, uint32_t* size)
{
    CONTAINER_STATUS lStatus = CONTAINER_SUCCESS;
    Container_h *lContainer = (Container_h *) pContainer;
    
    lStatus = container_isValid(lContainer);
    
    if(CONTAINER_SUCCESS == lStatus)
    {
        *size =  lContainer->m_size_data;
    }
    
    return lStatus;
}

void container_check_size(Container_h* pContainer)
{
    void** m_new_data;

    if (pContainer->m_size_data == pContainer->m_alloc_size)
    {
        pContainer->m_alloc_size = pContainer->m_alloc_size * 2;

        m_new_data = (void**) MEM_ALLOCATOR(sizeof(void*) * pContainer->m_alloc_size);
        memmove(m_new_data, pContainer->m_data, pContainer->m_size_data * sizeof(void*));
        MEM_DEALLOCATOR(pContainer->m_data);
        pContainer->m_data = m_new_data;
    }
}

void container_rebuild(Container_h* pContainer, uint32_t index)
{
    void** _zero = pContainer->m_data + index;
    void** _start_cpy = _zero + 1;
    int32_t size = (pContainer->m_size_data - 1) - index;
    if (size <= 0) return;
    memmove(_zero, _start_cpy, size * sizeof(void*));
}

CONTAINER_STATUS container_clear(void* pContainer)
{
    CONTAINER_STATUS lStatus = CONTAINER_SUCCESS;
    Container_h *lContainer = (Container_h *) pContainer;
    
    lStatus = container_isValid(lContainer);
    
    if(CONTAINER_SUCCESS == lStatus)
    {
        memset(lContainer->m_data, 0, lContainer->m_size_data);
        lContainer->m_size_data = 0;
    }
    
    return lStatus;
}


CONTAINER_STATUS container_flushMallocedContent(void* pContainer)
{
    CONTAINER_STATUS lStatus = CONTAINER_SUCCESS;
    Container_h *lContainer = (Container_h *) pContainer;
    uint32_t sz, i;
    void *old;

    lStatus = container_isValid(lContainer);
    
    if(CONTAINER_SUCCESS == lStatus)
    {
        old = NULL;
        sz = 0x00;
        container_size(pContainer, &sz);
        for (i = 0 ; i < sz;i++)
        {
            container_get(pContainer, i, &old);
            MEM_DEALLOCATOR(old);
        }
        container_clear(pContainer);
    }
    
    return lStatus;
}

CONTAINER_STATUS container_removePtr(void* pContainer, void* ref, void** out)
{
    CONTAINER_STATUS lStatus = CONTAINER_SUCCESS;
    Container_h *lContainer = (Container_h *) pContainer;
    uint32_t sz, i;
    void *scan;

    lStatus = container_isValid(lContainer);
    
    if(CONTAINER_SUCCESS == lStatus)
    {
        scan = NULL;
        sz = 0x00;
        container_size(pContainer, &sz);
        
        *out = NULL;
        for (i = 0; i < sz; i++)
        {
            container_get(pContainer, i, &scan);
            if (scan == ref)
            {
                container_remove(pContainer, i, out);
            }
        }
    }
    return lStatus;
}




CONTAINER_STATUS container_isValid(Container_h* pContainer)
{
    CONTAINER_STATUS lStatus = CONTAINER_SUCCESS;
    if (NULL == pContainer)
    {
        lStatus = CONTAINER_INVALID_PARAM;
    }
    if (CONTAINER_SUCCESS == lStatus)
    {
        if ((0x00 != STR_CMP(START_OK, pContainer->start, sizeof(START_OK))) || (0x00 != STR_CMP(END_OK, pContainer->end, sizeof(END_OK))))
        {
            lStatus = CONTAINER_INVALID_CONTAINER;
        }
    }
    return lStatus;
}