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
|
/*
* Motif
*
* Copyright (c) 1987-2012, The Open Group. All rights reserved.
*
* These libraries and programs are free software; you can
* redistribute them and/or modify them under the terms of the GNU
* Lesser General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* These libraries and programs are distributed in the hope that
* they will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with these librararies and programs; if not, write
* to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301 USA
*/
#ifdef REV_INFO
#ifndef lint
static char rcsid[] = "$XConsortium: Cache.c /main/12 1995/07/14 10:12:26 drk $"
#endif
#endif
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "CacheI.h"
#include <Xm/GadgetP.h>
/******** Static Function Declarations ********/
/******** End Static Function Declarations ********/
/************************************************************************
*
* _XmCacheDelete
* Delete an existing cache record. NOTE: <data> is a pointer to the
* fourth field in the cache record - It is *not* a pointer to the
* cache record itself!
*
************************************************************************/
void
_XmCacheDelete(
XtPointer data )
{
XmGadgetCachePtr ptr;
ptr = (XmGadgetCachePtr) DataToGadgetCache(data);
if (--ptr->ref_count <= 0) {
(ptr->prev)->next = ptr->next;
if (ptr->next) /* not the last record */
(ptr->next)->prev = ptr->prev;
XtFree( (char *) ptr );
}
}
/************************************************************************
*
* _XmCacheCopy
* Copy <size> bytes from <src> to <dest>.
*
************************************************************************/
void
_XmCacheCopy(
XtPointer src,
XtPointer dest,
size_t size )
{
memcpy( dest, src, size);
}
/************************************************************************
*
* _XmCachePart
* Pass in a pointer, <cpart>, to <size> bytes of a temporary Cache
* record.
* - If the Class cache head is NULL (no entries yet!), allocate a new
* cache record, copy in temporary Cache bytes, append it to the
* class-cache linked list, and return the address.
* - Else, run through the class linked list.
* = If a match is found, increment the ref_count and return the
* address.
* = Else, allocate a new cache record, copy in temporary Cache bytes,
* append it to the class-cache linked list, and return the address.
*
************************************************************************/
XtPointer
_XmCachePart(
XmCacheClassPartPtr cp,
XtPointer cpart,
size_t size )
{
XmGadgetCachePtr ptr, last;
if (ClassCacheHead(cp).next == NULL) /* First one */
{
ClassCacheHead(cp).next =
(struct _XmGadgetCache *)XtMalloc( size +
XtOffsetOf(XmGadgetCacheRef, data));
ptr = (XmGadgetCachePtr)ClassCacheHead(cp).next;
ClassCacheCopy(cp)(cpart, CacheDataPtr(ptr), size );
ptr-> ref_count = 1;
ptr-> next = NULL;
ptr-> prev = (struct _XmGadgetCache *)&ClassCacheHead(cp);
return (CacheDataPtr(ptr));
}
ptr = (XmGadgetCachePtr)ClassCacheHead(cp).next;
do
{
if ((ClassCacheCompare(cp)( cpart, CacheDataPtr(ptr))))
{
ptr->ref_count++;
return ((XtPointer) CacheDataPtr(ptr));
}
else
{
last = ptr;
ptr = (XmGadgetCachePtr)ptr->next;
}
} while (ptr);
/* Malloc a new rec off of last, fill it out*/
ptr = (XmGadgetCachePtr)XtMalloc( size +
XtOffsetOf(XmGadgetCacheRef, data) );
last->next = (struct _XmGadgetCache *)ptr;
ClassCacheCopy(cp)(cpart, CacheDataPtr(ptr), size);
ptr-> ref_count = 1;
ptr-> next = NULL;
ptr-> prev = last;
return (CacheDataPtr(ptr));
}
|