glib/glibbackend/src/lowmem.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19  
       
    20 #include "lowmem.h"
       
    21 #include "glibbackend_wsd.h"
       
    22 #include <pthread.h>
       
    23 #include <stdlib.h>
       
    24 
       
    25 #if EMULATOR
       
    26 
       
    27 PLS(key,lowmem,pthread_key_t)
       
    28 PLS(key_once,lowmem,pthread_once_t)
       
    29 
       
    30 #define key (*FUNCTION_NAME(key,lowmem )())
       
    31 #define key_once (*FUNCTION_NAME(key_once,lowmem )())
       
    32 
       
    33 #else
       
    34 pthread_key_t key;
       
    35 pthread_once_t key_once = PTHREAD_ONCE_INIT;
       
    36 #endif /* EMULATOR */
       
    37 
       
    38 static void make_key()
       
    39 {
       
    40 	pthread_key_create(&key,NULL);
       
    41 }
       
    42 
       
    43 EXPORT_C mem_info * get_thread_specific_data()
       
    44 {
       
    45 	pthread_once(&key_once, make_key);
       
    46 	return (mem_info *)pthread_getspecific(key);
       
    47 }
       
    48 
       
    49 /* This function sets the thread specfic data which is of the type 	*
       
    50  * struct mem_info.The function return 0 in case of success and a	*
       
    51  * non zero value in case of failure.								*
       
    52  */
       
    53 EXPORT_C int set_thread_specific_data(mem_info *m)
       
    54 {
       
    55 	pthread_once(&key_once, make_key);
       
    56 	if(pthread_setspecific(key,(void *)m))
       
    57 	{
       
    58 		return 1;
       
    59 	}
       
    60 	
       
    61 	return 0;
       
    62 }
       
    63 
       
    64 EXPORT_C int push(cleanUpStack *cs,void *ptr)
       
    65 {
       
    66 	if((*cs).top == 999)	
       
    67 		return -1;
       
    68 	else
       
    69 		(*cs).ptr[++((*cs).top)] = ptr;
       
    70 	return 0;
       
    71 }
       
    72 
       
    73 EXPORT_C void * pop(cleanUpStack *cs)
       
    74 {
       
    75 	return (*cs).ptr[((*cs).top)--];
       
    76 }
       
    77 
       
    78 EXPORT_C void clearCleanUpStack(cleanUpStack *cs)
       
    79 {
       
    80 	(*cs).top = -1;
       
    81 }
       
    82 
       
    83 EXPORT_C void destroyCleanUpStack(cleanUpStack *cs)
       
    84 {
       
    85 	while((*cs).top != -1)
       
    86 	{
       
    87 		free((*cs).ptr[((*cs).top)--]);
       
    88 	}
       
    89 }
       
    90 
       
    91 
       
    92 EXPORT_C void findAndDestroy(cleanUpStack *cs,void *ptr)
       
    93 {
       
    94 	int i;
       
    95 	for(i = 0; i < (*cs).top ; i++ )
       
    96 	{
       
    97 		if((*cs).ptr[i] == ptr)
       
    98 		{
       
    99 			(*cs).ptr[i] = NULL;
       
   100 			break;
       
   101 		}
       
   102 	}
       
   103 }