2014-06-26 07:41:25 -07:00
|
|
|
|
/*
|
2016-07-29 11:04:48 -07:00
|
|
|
|
* Copyright (c) 2014, 2016 Nicira, Inc.
|
2014-06-26 07:41:25 -07:00
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include "pvector.h"
|
|
|
|
|
|
2016-07-29 11:04:48 -07:00
|
|
|
|
/* Writers will preallocate space for some entries at the end to avoid future
|
|
|
|
|
* reallocations. */
|
|
|
|
|
enum { PVECTOR_EXTRA_ALLOC = 4 };
|
|
|
|
|
|
2016-07-29 11:04:49 -07:00
|
|
|
|
struct pvector *
|
|
|
|
|
pvector_alloc(size_t size)
|
2014-06-26 07:41:25 -07:00
|
|
|
|
{
|
2016-07-29 11:04:49 -07:00
|
|
|
|
struct pvector *pvec;
|
2014-06-26 07:41:25 -07:00
|
|
|
|
|
2016-07-29 11:04:49 -07:00
|
|
|
|
pvec = xmalloc(sizeof *pvec + size * sizeof pvec->vector[0]);
|
|
|
|
|
pvec->size = 0;
|
|
|
|
|
pvec->allocated = size;
|
2014-06-26 07:41:25 -07:00
|
|
|
|
|
2016-07-29 11:04:49 -07:00
|
|
|
|
return pvec;
|
2014-06-26 07:41:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-29 11:04:49 -07:00
|
|
|
|
static struct pvector *
|
|
|
|
|
pvector_dup(const struct pvector *old)
|
2014-06-26 07:41:25 -07:00
|
|
|
|
{
|
2016-07-29 11:04:49 -07:00
|
|
|
|
struct pvector *pvec = pvector_alloc(old->size + PVECTOR_EXTRA_ALLOC);
|
2014-06-26 07:41:25 -07:00
|
|
|
|
|
2016-07-29 11:04:49 -07:00
|
|
|
|
pvec->size = old->size;
|
|
|
|
|
memcpy(pvec->vector, old->vector, old->size * sizeof old->vector[0]);
|
|
|
|
|
return pvec;
|
2014-06-26 07:41:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-29 11:04:49 -07:00
|
|
|
|
/* Iterator for callers that need the 'index' afterward. */
|
|
|
|
|
#define PVECTOR_FOR_EACH_ENTRY(ENTRY, INDEX, IMPL) \
|
2014-06-26 07:41:25 -07:00
|
|
|
|
for ((INDEX) = 0; \
|
|
|
|
|
(INDEX) < (IMPL)->size \
|
|
|
|
|
&& ((ENTRY) = &(IMPL)->vector[INDEX], true); \
|
|
|
|
|
(INDEX)++)
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
pvector_entry_cmp(const void *a_, const void *b_)
|
|
|
|
|
{
|
2014-10-30 11:40:07 -07:00
|
|
|
|
const struct pvector_entry *ap = a_;
|
|
|
|
|
const struct pvector_entry *bp = b_;
|
|
|
|
|
int a = ap->priority;
|
|
|
|
|
int b = bp->priority;
|
2014-06-26 07:41:25 -07:00
|
|
|
|
|
|
|
|
|
return a > b ? -1 : a < b;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-29 11:04:49 -07:00
|
|
|
|
void
|
|
|
|
|
pvector_sort(struct pvector *pvec)
|
2014-06-26 07:41:25 -07:00
|
|
|
|
{
|
2016-07-29 11:04:49 -07:00
|
|
|
|
qsort(pvec->vector, pvec->size, sizeof *pvec->vector, pvector_entry_cmp);
|
2014-06-26 07:41:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Returns the index of the 'ptr' in the vector, or -1 if none is found. */
|
|
|
|
|
static int
|
2016-07-29 11:04:49 -07:00
|
|
|
|
pvector_find(const struct pvector *pvec, void *target)
|
2014-06-26 07:41:25 -07:00
|
|
|
|
{
|
|
|
|
|
const struct pvector_entry *entry;
|
|
|
|
|
int index;
|
|
|
|
|
|
2016-07-29 11:04:49 -07:00
|
|
|
|
PVECTOR_FOR_EACH_ENTRY (entry, index, pvec) {
|
2014-06-26 07:41:25 -07:00
|
|
|
|
if (entry->ptr == target) {
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-29 11:04:49 -07:00
|
|
|
|
/* May re-allocate 'impl' */
|
|
|
|
|
void
|
|
|
|
|
pvector_push_back(struct pvector **pvecp, void *ptr, int priority)
|
|
|
|
|
{
|
|
|
|
|
struct pvector *pvec = *pvecp;
|
|
|
|
|
|
|
|
|
|
if (pvec->size == pvec->allocated) {
|
|
|
|
|
pvec = pvector_dup(pvec);
|
|
|
|
|
free(*pvecp);
|
|
|
|
|
*pvecp = pvec;
|
|
|
|
|
}
|
|
|
|
|
/* Insert at the end, will be sorted later. */
|
|
|
|
|
pvec->vector[pvec->size].ptr = ptr;
|
|
|
|
|
pvec->vector[pvec->size].priority = priority;
|
|
|
|
|
pvec->size++;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-26 07:41:25 -07:00
|
|
|
|
void
|
2016-07-29 11:04:49 -07:00
|
|
|
|
pvector_remove(struct pvector *pvec, void *ptr)
|
2014-06-26 07:41:25 -07:00
|
|
|
|
{
|
2016-07-29 11:04:49 -07:00
|
|
|
|
int index;
|
|
|
|
|
|
|
|
|
|
index = pvector_find(pvec, ptr);
|
|
|
|
|
ovs_assert(index >= 0);
|
|
|
|
|
/* Now at the index of the entry to be deleted.
|
|
|
|
|
* Swap another entry in if needed, can be sorted later. */
|
|
|
|
|
pvec->size--;
|
|
|
|
|
if (index != pvec->size) {
|
|
|
|
|
pvec->vector[index] = pvec->vector[pvec->size];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Concurrent version. */
|
|
|
|
|
|
|
|
|
|
/* Initializes 'cpvec' as an empty concurrent priority vector. */
|
|
|
|
|
void
|
|
|
|
|
cpvector_init(struct cpvector *cpvec)
|
|
|
|
|
{
|
|
|
|
|
ovsrcu_set(&cpvec->impl, pvector_alloc(PVECTOR_EXTRA_ALLOC));
|
|
|
|
|
cpvec->temp = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Destroys 'cpvec'.
|
|
|
|
|
*
|
|
|
|
|
* The client is responsible for destroying any data previously held in
|
|
|
|
|
* 'pvec'. */
|
|
|
|
|
void
|
|
|
|
|
cpvector_destroy(struct cpvector *cpvec)
|
|
|
|
|
{
|
|
|
|
|
free(cpvec->temp);
|
|
|
|
|
cpvec->temp = NULL;
|
|
|
|
|
ovsrcu_postpone(free, cpvector_get_pvector(cpvec));
|
|
|
|
|
ovsrcu_set(&cpvec->impl, NULL); /* Poison. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
cpvector_insert(struct cpvector *cpvec, void *ptr, int priority)
|
|
|
|
|
{
|
|
|
|
|
struct pvector *temp = cpvec->temp;
|
|
|
|
|
struct pvector *old = cpvector_get_pvector(cpvec);
|
2014-06-26 07:41:25 -07:00
|
|
|
|
|
|
|
|
|
ovs_assert(ptr != NULL);
|
|
|
|
|
|
|
|
|
|
/* Check if can add to the end without reallocation. */
|
2014-11-13 11:54:31 -08:00
|
|
|
|
if (!temp && old->allocated > old->size &&
|
2014-06-26 07:41:25 -07:00
|
|
|
|
(!old->size || priority <= old->vector[old->size - 1].priority)) {
|
|
|
|
|
old->vector[old->size].ptr = ptr;
|
|
|
|
|
old->vector[old->size].priority = priority;
|
|
|
|
|
/* Size increment must not be visible to the readers before the new
|
|
|
|
|
* entry is stored. */
|
|
|
|
|
atomic_thread_fence(memory_order_release);
|
|
|
|
|
++old->size;
|
|
|
|
|
} else {
|
2014-11-13 11:54:31 -08:00
|
|
|
|
if (!temp) {
|
2016-07-29 11:04:49 -07:00
|
|
|
|
cpvec->temp = pvector_dup(old);
|
2014-11-13 11:54:31 -08:00
|
|
|
|
}
|
2016-07-29 11:04:49 -07:00
|
|
|
|
pvector_push_back(&cpvec->temp, ptr, priority);
|
2014-06-26 07:41:25 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-07-29 11:04:49 -07:00
|
|
|
|
cpvector_remove(struct cpvector *cpvec, void *ptr)
|
2014-06-26 07:41:25 -07:00
|
|
|
|
{
|
2016-07-29 11:04:49 -07:00
|
|
|
|
struct pvector *temp = cpvec->temp;
|
2014-06-26 07:41:25 -07:00
|
|
|
|
|
2014-11-13 11:54:31 -08:00
|
|
|
|
if (!temp) {
|
2016-07-29 11:04:49 -07:00
|
|
|
|
temp = pvector_dup(cpvector_get_pvector(cpvec));
|
|
|
|
|
cpvec->temp = temp;
|
2014-11-13 11:54:31 -08:00
|
|
|
|
}
|
|
|
|
|
ovs_assert(temp->size > 0);
|
2016-07-29 11:04:49 -07:00
|
|
|
|
pvector_remove(temp, ptr); /* Publish will sort. */
|
2014-06-26 07:41:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Change entry's 'priority' and keep the vector ordered. */
|
|
|
|
|
void
|
2016-07-29 11:04:49 -07:00
|
|
|
|
cpvector_change_priority(struct cpvector *cpvec, void *ptr, int priority)
|
2014-06-26 07:41:25 -07:00
|
|
|
|
{
|
2016-07-29 11:04:49 -07:00
|
|
|
|
struct pvector *old = cpvec->temp;
|
2014-11-13 11:54:31 -08:00
|
|
|
|
int index;
|
|
|
|
|
|
|
|
|
|
if (!old) {
|
2016-07-29 11:04:49 -07:00
|
|
|
|
old = cpvector_get_pvector(cpvec);
|
2014-11-13 11:54:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-29 11:04:49 -07:00
|
|
|
|
index = pvector_find(old, ptr);
|
2014-06-26 07:41:25 -07:00
|
|
|
|
|
|
|
|
|
ovs_assert(index >= 0);
|
|
|
|
|
/* Now at the index of the entry to be updated. */
|
|
|
|
|
|
2014-11-13 11:54:31 -08:00
|
|
|
|
/* Check if can not update in place. */
|
2014-06-26 07:41:25 -07:00
|
|
|
|
if ((priority > old->vector[index].priority && index > 0
|
|
|
|
|
&& priority > old->vector[index - 1].priority)
|
|
|
|
|
|| (priority < old->vector[index].priority && index < old->size - 1
|
|
|
|
|
&& priority < old->vector[index + 1].priority)) {
|
2014-11-13 11:54:31 -08:00
|
|
|
|
/* Have to use a temp. */
|
2016-07-29 11:04:49 -07:00
|
|
|
|
if (!cpvec->temp) {
|
2014-11-13 11:54:31 -08:00
|
|
|
|
/* Have to reallocate to reorder. */
|
2016-07-29 11:04:49 -07:00
|
|
|
|
cpvec->temp = pvector_dup(old);
|
|
|
|
|
old = cpvec->temp;
|
2014-11-13 11:54:31 -08:00
|
|
|
|
/* Publish will sort. */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
old->vector[index].priority = priority;
|
|
|
|
|
}
|
2014-06-26 07:41:25 -07:00
|
|
|
|
|
2014-11-13 11:54:31 -08:00
|
|
|
|
/* Make the modified pvector available for iteration. */
|
2016-07-29 11:04:49 -07:00
|
|
|
|
void cpvector_publish__(struct cpvector *cpvec)
|
2014-11-13 11:54:31 -08:00
|
|
|
|
{
|
2016-07-29 11:04:49 -07:00
|
|
|
|
struct pvector *temp = cpvec->temp;
|
2014-06-26 07:41:25 -07:00
|
|
|
|
|
2016-07-29 11:04:49 -07:00
|
|
|
|
cpvec->temp = NULL;
|
|
|
|
|
pvector_sort(temp); /* Also removes gaps. */
|
|
|
|
|
ovsrcu_postpone(free, ovsrcu_get_protected(struct pvector *,
|
|
|
|
|
&cpvec->impl));
|
|
|
|
|
ovsrcu_set(&cpvec->impl, temp);
|
2014-06-26 07:41:25 -07:00
|
|
|
|
}
|