101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2015 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2101e04c3fSmrg * IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg#include <string.h>
2501e04c3fSmrg#include "util/u_math.h"
2601e04c3fSmrg#include "util/u_vector.h"
2701e04c3fSmrg
2801e04c3fSmrg/** @file u_vector.c
2901e04c3fSmrg *
3001e04c3fSmrg * A dynamically growable, circular buffer.  Elements are added at head and
3101e04c3fSmrg * removed from tail. head and tail are free-running uint32_t indices and we
3201e04c3fSmrg * only compute the modulo with size when accessing the array.  This way,
3301e04c3fSmrg * number of bytes in the queue is always head - tail, even in case of
3401e04c3fSmrg * wraparound.
3501e04c3fSmrg */
3601e04c3fSmrg
377ec681f3Smrg/**
387ec681f3Smrg * initial_element_count and element_size must be power-of-two.
397ec681f3Smrg */
4001e04c3fSmrgint
417ec681f3Smrgu_vector_init_pow2(struct u_vector *vector,
427ec681f3Smrg                   uint32_t initial_element_count,
437ec681f3Smrg                   uint32_t element_size)
4401e04c3fSmrg{
457ec681f3Smrg   assert(util_is_power_of_two_nonzero(initial_element_count));
467ec681f3Smrg   assert(util_is_power_of_two_nonzero(element_size));
4701e04c3fSmrg
4801e04c3fSmrg   vector->head = 0;
4901e04c3fSmrg   vector->tail = 0;
5001e04c3fSmrg   vector->element_size = element_size;
517ec681f3Smrg   vector->size = element_size * initial_element_count;
527ec681f3Smrg   vector->data = malloc(vector->size);
5301e04c3fSmrg
5401e04c3fSmrg   return vector->data != NULL;
5501e04c3fSmrg}
5601e04c3fSmrg
5701e04c3fSmrgvoid *
5801e04c3fSmrgu_vector_add(struct u_vector *vector)
5901e04c3fSmrg{
6001e04c3fSmrg   uint32_t offset, size, split, src_tail, dst_tail;
6101e04c3fSmrg   void *data;
6201e04c3fSmrg
6301e04c3fSmrg   if (vector->head - vector->tail == vector->size) {
6401e04c3fSmrg      size = vector->size * 2;
6501e04c3fSmrg      data = malloc(size);
6601e04c3fSmrg      if (data == NULL)
6701e04c3fSmrg         return NULL;
6801e04c3fSmrg      src_tail = vector->tail & (vector->size - 1);
6901e04c3fSmrg      dst_tail = vector->tail & (size - 1);
7001e04c3fSmrg      if (src_tail == 0) {
7101e04c3fSmrg         /* Since we know that the vector is full, this means that it's
7201e04c3fSmrg          * linear from start to end so we can do one copy.
7301e04c3fSmrg          */
7401e04c3fSmrg         memcpy((char *)data + dst_tail, vector->data, vector->size);
7501e04c3fSmrg      } else {
7601e04c3fSmrg         /* In this case, the vector is split into two pieces and we have
7701e04c3fSmrg          * to do two copies.  We have to be careful to make sure each
7801e04c3fSmrg          * piece goes to the right locations.  Thanks to the change in
7901e04c3fSmrg          * size, it may or may not still wrap around.
8001e04c3fSmrg          */
8101e04c3fSmrg         split = u_align_u32(vector->tail, vector->size);
8201e04c3fSmrg         assert(vector->tail <= split && split < vector->head);
8301e04c3fSmrg         memcpy((char *)data + dst_tail, (char *)vector->data + src_tail,
8401e04c3fSmrg                split - vector->tail);
8501e04c3fSmrg         memcpy((char *)data + (split & (size - 1)), vector->data,
8601e04c3fSmrg                vector->head - split);
8701e04c3fSmrg      }
8801e04c3fSmrg      free(vector->data);
8901e04c3fSmrg      vector->data = data;
9001e04c3fSmrg      vector->size = size;
9101e04c3fSmrg   }
9201e04c3fSmrg
9301e04c3fSmrg   assert(vector->head - vector->tail < vector->size);
9401e04c3fSmrg
9501e04c3fSmrg   offset = vector->head & (vector->size - 1);
9601e04c3fSmrg   vector->head += vector->element_size;
9701e04c3fSmrg
9801e04c3fSmrg   return (char *)vector->data + offset;
9901e04c3fSmrg}
10001e04c3fSmrg
10101e04c3fSmrgvoid *
10201e04c3fSmrgu_vector_remove(struct u_vector *vector)
10301e04c3fSmrg{
10401e04c3fSmrg   uint32_t offset;
10501e04c3fSmrg
10601e04c3fSmrg   if (vector->head == vector->tail)
10701e04c3fSmrg      return NULL;
10801e04c3fSmrg
10901e04c3fSmrg   assert(vector->head - vector->tail <= vector->size);
11001e04c3fSmrg
11101e04c3fSmrg   offset = vector->tail & (vector->size - 1);
11201e04c3fSmrg   vector->tail += vector->element_size;
11301e04c3fSmrg
11401e04c3fSmrg   return (char *)vector->data + offset;
11501e04c3fSmrg}
116