pb_bufmgr_cache.c

Go to the documentation of this file.
00001 /**************************************************************************
00002  *
00003  * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
00004  * All Rights Reserved.
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  *
00014  * The above copyright notice and this permission notice (including the
00015  * next paragraph) shall be included in all copies or substantial portions
00016  * of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00020  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
00021  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
00022  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00023  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00024  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  *
00026  **************************************************************************/
00027 
00037 #include "pipe/p_compiler.h"
00038 #include "pipe/p_debug.h"
00039 #include "pipe/p_winsys.h"
00040 #include "pipe/p_thread.h"
00041 #include "util/u_memory.h"
00042 #include "util/u_double_list.h"
00043 #include "util/u_time.h"
00044 
00045 #include "pb_buffer.h"
00046 #include "pb_bufmgr.h"
00047 
00048 
00052 #define SUPER(__derived) (&(__derived)->base)
00053 
00054 
00055 struct pb_cache_manager;
00056 
00057 
00061 struct pb_cache_buffer
00062 {
00063    struct pb_buffer base;
00064    
00065    struct pb_buffer *buffer;
00066    struct pb_cache_manager *mgr;
00067 
00069    struct util_time start, end;
00070 
00071    struct list_head head;
00072 };
00073 
00074 
00075 struct pb_cache_manager
00076 {
00077    struct pb_manager base;
00078 
00079    struct pb_manager *provider;
00080    unsigned usecs;
00081    
00082    pipe_mutex mutex;
00083    
00084    struct list_head delayed;
00085    size_t numDelayed;
00086 };
00087 
00088 
00089 static INLINE struct pb_cache_buffer *
00090 pb_cache_buffer(struct pb_buffer *buf)
00091 {
00092    assert(buf);
00093    return (struct pb_cache_buffer *)buf;
00094 }
00095 
00096 
00097 static INLINE struct pb_cache_manager *
00098 pb_cache_manager(struct pb_manager *mgr)
00099 {
00100    assert(mgr);
00101    return (struct pb_cache_manager *)mgr;
00102 }
00103 
00104 
00108 static INLINE void
00109 _pb_cache_buffer_destroy(struct pb_cache_buffer *buf)
00110 {
00111    struct pb_cache_manager *mgr = buf->mgr;
00112 
00113    LIST_DEL(&buf->head);
00114    assert(mgr->numDelayed);
00115    --mgr->numDelayed;
00116    assert(!buf->base.base.refcount);
00117    pb_reference(&buf->buffer, NULL);
00118    FREE(buf);
00119 }
00120 
00121 
00125 static void
00126 _pb_cache_buffer_list_check_free(struct pb_cache_manager *mgr)
00127 {
00128    struct list_head *curr, *next;
00129    struct pb_cache_buffer *buf;
00130    struct util_time now;
00131    
00132    util_time_get(&now);
00133    
00134    curr = mgr->delayed.next;
00135    next = curr->next;
00136    while(curr != &mgr->delayed) {
00137       buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
00138 
00139       if(!util_time_timeout(&buf->start, &buf->end, &now))
00140          break;
00141          
00142       _pb_cache_buffer_destroy(buf);
00143 
00144       curr = next; 
00145       next = curr->next;
00146    }
00147 }
00148 
00149 
00150 static void
00151 pb_cache_buffer_destroy(struct pb_buffer *_buf)
00152 {
00153    struct pb_cache_buffer *buf = pb_cache_buffer(_buf);   
00154    struct pb_cache_manager *mgr = buf->mgr;
00155 
00156    pipe_mutex_lock(mgr->mutex);
00157    assert(buf->base.base.refcount == 0);
00158    
00159    _pb_cache_buffer_list_check_free(mgr);
00160    
00161    util_time_get(&buf->start);
00162    util_time_add(&buf->start, mgr->usecs, &buf->end);
00163    LIST_ADDTAIL(&buf->head, &mgr->delayed);
00164    ++mgr->numDelayed;
00165    pipe_mutex_unlock(mgr->mutex);
00166 }
00167 
00168 
00169 static void *
00170 pb_cache_buffer_map(struct pb_buffer *_buf, 
00171                   unsigned flags)
00172 {
00173    struct pb_cache_buffer *buf = pb_cache_buffer(_buf);   
00174    return pb_map(buf->buffer, flags);
00175 }
00176 
00177 
00178 static void
00179 pb_cache_buffer_unmap(struct pb_buffer *_buf)
00180 {
00181    struct pb_cache_buffer *buf = pb_cache_buffer(_buf);   
00182    pb_unmap(buf->buffer);
00183 }
00184 
00185 
00186 static void
00187 pb_cache_buffer_get_base_buffer(struct pb_buffer *_buf,
00188                               struct pb_buffer **base_buf,
00189                               unsigned *offset)
00190 {
00191    struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
00192    pb_get_base_buffer(buf->buffer, base_buf, offset);
00193 }
00194 
00195 
00196 const struct pb_vtbl 
00197 pb_cache_buffer_vtbl = {
00198       pb_cache_buffer_destroy,
00199       pb_cache_buffer_map,
00200       pb_cache_buffer_unmap,
00201       pb_cache_buffer_get_base_buffer
00202 };
00203 
00204 
00205 static INLINE boolean
00206 pb_cache_is_buffer_compat(struct pb_cache_buffer *buf,  
00207                           size_t size,
00208                           const struct pb_desc *desc)
00209 {
00210    if(buf->base.base.size < size)
00211       return FALSE;
00212 
00213    /* be lenient with size */
00214    if(buf->base.base.size >= 2*size)
00215       return FALSE;
00216    
00217    if(!pb_check_alignment(desc->alignment, buf->base.base.alignment))
00218       return FALSE;
00219    
00220    if(!pb_check_usage(desc->usage, buf->base.base.usage))
00221       return FALSE;
00222    
00223    return TRUE;
00224 }
00225 
00226 
00227 static struct pb_buffer *
00228 pb_cache_manager_create_buffer(struct pb_manager *_mgr, 
00229                                size_t size,
00230                                const struct pb_desc *desc)
00231 {
00232    struct pb_cache_manager *mgr = pb_cache_manager(_mgr);
00233    struct pb_cache_buffer *buf;
00234    struct pb_cache_buffer *curr_buf;
00235    struct list_head *curr, *next;
00236    struct util_time now;
00237    
00238    pipe_mutex_lock(mgr->mutex);
00239 
00240    buf = NULL;
00241    curr = mgr->delayed.next;
00242    next = curr->next;
00243    
00244    /* search in the expired buffers, freeing them in the process */
00245    util_time_get(&now);
00246    while(curr != &mgr->delayed) {
00247       curr_buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
00248       if(!buf && pb_cache_is_buffer_compat(curr_buf, size, desc))
00249          buf = curr_buf;
00250       else if(util_time_timeout(&curr_buf->start, &curr_buf->end, &now))
00251          _pb_cache_buffer_destroy(curr_buf);
00252       else
00253          /* This buffer (and all hereafter) are still hot in cache */
00254          break;
00255       curr = next; 
00256       next = curr->next;
00257    }
00258 
00259    /* keep searching in the hot buffers */
00260    if(!buf) {
00261       while(curr != &mgr->delayed) {
00262          curr_buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
00263          if(pb_cache_is_buffer_compat(curr_buf, size, desc)) {
00264             buf = curr_buf;
00265             break;
00266          }
00267          /* no need to check the timeout here */
00268          curr = next;
00269          next = curr->next;
00270       }
00271    }
00272    
00273    if(buf) {
00274       LIST_DEL(&buf->head);
00275       pipe_mutex_unlock(mgr->mutex);
00276       ++buf->base.base.refcount;
00277       return &buf->base;
00278    }
00279    
00280    pipe_mutex_unlock(mgr->mutex);
00281 
00282    buf = CALLOC_STRUCT(pb_cache_buffer);
00283    if(!buf)
00284       return NULL;
00285    
00286    buf->buffer = mgr->provider->create_buffer(mgr->provider, size, desc);
00287    if(!buf->buffer) {
00288       FREE(buf);
00289       return NULL;
00290    }
00291    
00292    assert(buf->buffer->base.refcount >= 1);
00293    assert(pb_check_alignment(desc->alignment, buf->buffer->base.alignment));
00294    assert(pb_check_usage(desc->usage, buf->buffer->base.usage));
00295    assert(buf->buffer->base.size >= size);
00296    
00297    buf->base.base.refcount = 1;
00298    buf->base.base.alignment = buf->buffer->base.alignment;
00299    buf->base.base.usage = buf->buffer->base.usage;
00300    buf->base.base.size = buf->buffer->base.size;
00301    
00302    buf->base.vtbl = &pb_cache_buffer_vtbl;
00303    buf->mgr = mgr;
00304    
00305    return &buf->base;
00306 }
00307 
00308 
00309 static void
00310 pb_cache_manager_flush(struct pb_manager *_mgr)
00311 {
00312    struct pb_cache_manager *mgr = pb_cache_manager(_mgr);
00313    struct list_head *curr, *next;
00314    struct pb_cache_buffer *buf;
00315 
00316    pipe_mutex_lock(mgr->mutex);
00317    curr = mgr->delayed.next;
00318    next = curr->next;
00319    while(curr != &mgr->delayed) {
00320       buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
00321       _pb_cache_buffer_destroy(buf);
00322       curr = next; 
00323       next = curr->next;
00324    }
00325    pipe_mutex_unlock(mgr->mutex);
00326    
00327    assert(mgr->provider->flush);
00328    if(mgr->provider->flush)
00329       mgr->provider->flush(mgr->provider);
00330 }
00331 
00332 
00333 static void
00334 pb_cache_manager_destroy(struct pb_manager *mgr)
00335 {
00336    pb_cache_manager_flush(mgr);
00337    FREE(mgr);
00338 }
00339 
00340 
00341 struct pb_manager *
00342 pb_cache_manager_create(struct pb_manager *provider, 
00343                         unsigned usecs) 
00344 {
00345    struct pb_cache_manager *mgr;
00346 
00347    if(!provider)
00348       return NULL;
00349    
00350    mgr = CALLOC_STRUCT(pb_cache_manager);
00351    if (!mgr)
00352       return NULL;
00353 
00354    mgr->base.destroy = pb_cache_manager_destroy;
00355    mgr->base.create_buffer = pb_cache_manager_create_buffer;
00356    mgr->base.flush = pb_cache_manager_flush;
00357    mgr->provider = provider;
00358    mgr->usecs = usecs;
00359    LIST_INITHEAD(&mgr->delayed);
00360    mgr->numDelayed = 0;
00361    pipe_mutex_init(mgr->mutex);
00362       
00363    return &mgr->base;
00364 }

Generated on Tue Sep 29 06:25:14 2009 for Gallium3D by  doxygen 1.5.4