Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / gallium / auxiliary / pipebuffer / pb_cache.c
index adae22270aad6627e23c7f5a8ffbf2eddfe9e6d9..ec878202f0825b266e1d862819404a1e31439c80 100644 (file)
@@ -28,7 +28,7 @@
 
 #include "pb_cache.h"
 #include "util/u_memory.h"
-#include "util/u_time.h"
+#include "util/os_time.h"
 
 
 /**
@@ -42,7 +42,7 @@ destroy_buffer_locked(struct pb_cache_entry *entry)
 
    assert(!pipe_is_referenced(&buf->reference));
    if (entry->head.next) {
-      LIST_DEL(&entry->head);
+      list_del(&entry->head);
       assert(mgr->num_buffers);
       --mgr->num_buffers;
       mgr->cache_size -= buf->size;
@@ -54,20 +54,18 @@ destroy_buffer_locked(struct pb_cache_entry *entry)
  * Free as many cache buffers from the list head as possible.
  */
 static void
-release_expired_buffers_locked(struct list_head *cache)
+release_expired_buffers_locked(struct list_head *cache,
+                               int64_t current_time)
 {
    struct list_head *curr, *next;
    struct pb_cache_entry *entry;
-   int64_t now;
-
-   now = os_time_get();
 
    curr = cache->next;
    next = curr->next;
    while (curr != cache) {
       entry = LIST_ENTRY(struct pb_cache_entry, curr, head);
 
-      if (!os_time_timeout(entry->start, entry->end, now))
+      if (!os_time_timeout(entry->start, entry->end, current_time))
          break;
 
       destroy_buffer_locked(entry);
@@ -89,25 +87,27 @@ pb_cache_add_buffer(struct pb_cache_entry *entry)
    struct pb_buffer *buf = entry->buffer;
    unsigned i;
 
-   pipe_mutex_lock(mgr->mutex);
+   mtx_lock(&mgr->mutex);
    assert(!pipe_is_referenced(&buf->reference));
 
-   for (i = 0; i < ARRAY_SIZE(mgr->buckets); i++)
-      release_expired_buffers_locked(&mgr->buckets[i]);
+   int64_t current_time = os_time_get();
+
+   for (i = 0; i < mgr->num_heaps; i++)
+      release_expired_buffers_locked(&mgr->buckets[i], current_time);
 
    /* Directly release any buffer that exceeds the limit. */
    if (mgr->cache_size + buf->size > mgr->max_cache_size) {
       mgr->destroy_buffer(buf);
-      pipe_mutex_unlock(mgr->mutex);
+      mtx_unlock(&mgr->mutex);
       return;
    }
 
    entry->start = os_time_get();
    entry->end = entry->start + mgr->usecs;
-   LIST_ADDTAIL(&entry->head, cache);
+   list_addtail(&entry->head, cache);
    ++mgr->num_buffers;
    mgr->cache_size += buf->size;
-   pipe_mutex_unlock(mgr->mutex);
+   mtx_unlock(&mgr->mutex);
 }
 
 /**
@@ -153,9 +153,11 @@ pb_cache_reclaim_buffer(struct pb_cache *mgr, pb_size size,
    struct list_head *cur, *next;
    int64_t now;
    int ret = 0;
+
+   assert(bucket_index < mgr->num_heaps);
    struct list_head *cache = &mgr->buckets[bucket_index];
 
-   pipe_mutex_lock(mgr->mutex);
+   mtx_lock(&mgr->mutex);
 
    entry = NULL;
    cur = cache->next;
@@ -167,7 +169,7 @@ pb_cache_reclaim_buffer(struct pb_cache *mgr, pb_size size,
       cur_entry = LIST_ENTRY(struct pb_cache_entry, cur, head);
 
       if (!entry && (ret = pb_cache_is_buffer_compat(cur_entry, size,
-                                                     alignment, usage) > 0))
+                                                     alignment, usage)) > 0)
          entry = cur_entry;
       else if (os_time_timeout(cur_entry->start, cur_entry->end, now))
          destroy_buffer_locked(cur_entry);
@@ -206,15 +208,15 @@ pb_cache_reclaim_buffer(struct pb_cache *mgr, pb_size size,
       struct pb_buffer *buf = entry->buffer;
 
       mgr->cache_size -= buf->size;
-      LIST_DEL(&entry->head);
+      list_del(&entry->head);
       --mgr->num_buffers;
-      pipe_mutex_unlock(mgr->mutex);
+      mtx_unlock(&mgr->mutex);
       /* Increase refcount */
       pipe_reference_init(&buf->reference, 1);
       return buf;
    }
 
-   pipe_mutex_unlock(mgr->mutex);
+   mtx_unlock(&mgr->mutex);
    return NULL;
 }
 
@@ -228,8 +230,8 @@ pb_cache_release_all_buffers(struct pb_cache *mgr)
    struct pb_cache_entry *buf;
    unsigned i;
 
-   pipe_mutex_lock(mgr->mutex);
-   for (i = 0; i < ARRAY_SIZE(mgr->buckets); i++) {
+   mtx_lock(&mgr->mutex);
+   for (i = 0; i < mgr->num_heaps; i++) {
       struct list_head *cache = &mgr->buckets[i];
 
       curr = cache->next;
@@ -241,13 +243,15 @@ pb_cache_release_all_buffers(struct pb_cache *mgr)
          next = curr->next;
       }
    }
-   pipe_mutex_unlock(mgr->mutex);
+   mtx_unlock(&mgr->mutex);
 }
 
 void
 pb_cache_init_entry(struct pb_cache *mgr, struct pb_cache_entry *entry,
                     struct pb_buffer *buf, unsigned bucket_index)
 {
+   assert(bucket_index < mgr->num_heaps);
+
    memset(entry, 0, sizeof(*entry));
    entry->buffer = buf;
    entry->mgr = mgr;
@@ -258,6 +262,9 @@ pb_cache_init_entry(struct pb_cache *mgr, struct pb_cache_entry *entry,
  * Initialize a caching buffer manager.
  *
  * @param mgr     The cache buffer manager
+ * @param num_heaps  Number of separate caches/buckets indexed by bucket_index
+ *                   for faster buffer matching (alternative to slower
+ *                   "usage"-based matching).
  * @param usecs   Unused buffers may be released from the cache after this
  *                time
  * @param size_factor  Declare buffers that are size_factor times bigger than
@@ -270,19 +277,25 @@ pb_cache_init_entry(struct pb_cache *mgr, struct pb_cache_entry *entry,
  * @param can_reclaim     Whether a buffer can be reclaimed (e.g. is not busy)
  */
 void
-pb_cache_init(struct pb_cache *mgr, uint usecs, float size_factor,
+pb_cache_init(struct pb_cache *mgr, uint num_heaps,
+              uint usecs, float size_factor,
               unsigned bypass_usage, uint64_t maximum_cache_size,
               void (*destroy_buffer)(struct pb_buffer *buf),
               bool (*can_reclaim)(struct pb_buffer *buf))
 {
    unsigned i;
 
-   for (i = 0; i < ARRAY_SIZE(mgr->buckets); i++)
-      LIST_INITHEAD(&mgr->buckets[i]);
+   mgr->buckets = CALLOC(num_heaps, sizeof(struct list_head));
+   if (!mgr->buckets)
+      return;
+
+   for (i = 0; i < num_heaps; i++)
+      list_inithead(&mgr->buckets[i]);
 
    (void) mtx_init(&mgr->mutex, mtx_plain);
    mgr->cache_size = 0;
    mgr->max_cache_size = maximum_cache_size;
+   mgr->num_heaps = num_heaps;
    mgr->usecs = usecs;
    mgr->num_buffers = 0;
    mgr->bypass_usage = bypass_usage;
@@ -299,4 +312,6 @@ pb_cache_deinit(struct pb_cache *mgr)
 {
    pb_cache_release_all_buffers(mgr);
    mtx_destroy(&mgr->mutex);
+   FREE(mgr->buckets);
+   mgr->buckets = NULL;
 }