2014-06-26 07:41:25 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Nicira, Inc.
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
static struct pvector_impl *
|
|
|
|
pvector_impl_get(const struct pvector *pvec)
|
|
|
|
{
|
|
|
|
return ovsrcu_get(struct pvector_impl *, &pvec->impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct pvector_impl *
|
|
|
|
pvector_impl_alloc(size_t size)
|
|
|
|
{
|
|
|
|
struct pvector_impl *impl;
|
|
|
|
|
|
|
|
impl = xmalloc(sizeof *impl + size * sizeof impl->vector[0]);
|
|
|
|
impl->size = 0;
|
|
|
|
impl->allocated = size;
|
|
|
|
|
|
|
|
return impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct pvector_impl *
|
|
|
|
pvector_impl_dup(struct pvector_impl *old)
|
|
|
|
{
|
2014-11-13 11:54:31 -08:00
|
|
|
struct pvector_impl *impl;
|
|
|
|
size_t alloc = old->size + PVECTOR_EXTRA_ALLOC;
|
|
|
|
|
|
|
|
impl = xmalloc(sizeof *impl + alloc * sizeof impl->vector[0]);
|
|
|
|
impl->allocated = alloc;
|
|
|
|
impl->size = old->size;
|
|
|
|
memcpy(impl->vector, old->vector, old->size * sizeof old->vector[0]);
|
|
|
|
return impl;
|
2014-06-26 07:41:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initializes 'pvec' as an empty concurrent priority vector. */
|
|
|
|
void
|
|
|
|
pvector_init(struct pvector *pvec)
|
|
|
|
{
|
|
|
|
ovsrcu_set(&pvec->impl, pvector_impl_alloc(PVECTOR_EXTRA_ALLOC));
|
2014-11-13 11:54:31 -08:00
|
|
|
pvec->temp = NULL;
|
2014-06-26 07:41:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroys 'pvec'.
|
|
|
|
*
|
|
|
|
* The client is responsible for destroying any data previously held in
|
|
|
|
* 'pvec'. */
|
|
|
|
void
|
|
|
|
pvector_destroy(struct pvector *pvec)
|
|
|
|
{
|
2014-11-13 11:54:31 -08:00
|
|
|
free(pvec->temp);
|
|
|
|
pvec->temp = NULL;
|
2014-06-26 07:41:25 -07:00
|
|
|
ovsrcu_postpone(free, pvector_impl_get(pvec));
|
|
|
|
ovsrcu_set(&pvec->impl, NULL); /* Poison. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Iterators for callers that need the 'index' afterward. */
|
|
|
|
#define PVECTOR_IMPL_FOR_EACH(ENTRY, INDEX, IMPL) \
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pvector_impl_sort(struct pvector_impl *impl)
|
|
|
|
{
|
|
|
|
qsort(impl->vector, impl->size, sizeof *impl->vector, pvector_entry_cmp);
|
2014-11-13 11:54:31 -08:00
|
|
|
/* Trim gaps. */
|
|
|
|
while (impl->size && impl->vector[impl->size - 1].priority == INT_MIN) {
|
|
|
|
impl->size = impl->size - 1;
|
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
|
|
|
|
pvector_impl_find(struct pvector_impl *impl, void *target)
|
|
|
|
{
|
|
|
|
const struct pvector_entry *entry;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
PVECTOR_IMPL_FOR_EACH (entry, index, impl) {
|
|
|
|
if (entry->ptr == target) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-10-30 11:40:07 -07:00
|
|
|
pvector_insert(struct pvector *pvec, void *ptr, int priority)
|
2014-06-26 07:41:25 -07:00
|
|
|
{
|
2014-11-13 11:54:31 -08:00
|
|
|
struct pvector_impl *temp = pvec->temp;
|
|
|
|
struct pvector_impl *old = pvector_impl_get(pvec);
|
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) {
|
|
|
|
temp = pvector_impl_dup(old);
|
|
|
|
pvec->temp = temp;
|
|
|
|
} else if (temp->size == temp->allocated) {
|
|
|
|
temp = pvector_impl_dup(temp);
|
|
|
|
free(pvec->temp);
|
|
|
|
pvec->temp = temp;
|
|
|
|
}
|
|
|
|
/* Insert at the end, publish will sort. */
|
|
|
|
temp->vector[temp->size].ptr = ptr;
|
|
|
|
temp->vector[temp->size].priority = priority;
|
|
|
|
temp->size += 1;
|
2014-06-26 07:41:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pvector_remove(struct pvector *pvec, void *ptr)
|
|
|
|
{
|
2014-11-13 11:54:31 -08:00
|
|
|
struct pvector_impl *temp = pvec->temp;
|
2014-06-26 07:41:25 -07:00
|
|
|
int index;
|
|
|
|
|
2014-11-13 11:54:31 -08:00
|
|
|
if (!temp) {
|
|
|
|
temp = pvector_impl_dup(pvector_impl_get(pvec));
|
|
|
|
pvec->temp = temp;
|
|
|
|
}
|
|
|
|
ovs_assert(temp->size > 0);
|
|
|
|
index = pvector_impl_find(temp, ptr);
|
2014-06-26 07:41:25 -07:00
|
|
|
ovs_assert(index >= 0);
|
2014-11-13 11:54:31 -08:00
|
|
|
/* Now at the index of the entry to be deleted.
|
|
|
|
* Clear in place, publish will sort and clean these off. */
|
|
|
|
temp->vector[index].ptr = NULL;
|
|
|
|
temp->vector[index].priority = INT_MIN;
|
2014-06-26 07:41:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Change entry's 'priority' and keep the vector ordered. */
|
|
|
|
void
|
2014-10-30 11:40:07 -07:00
|
|
|
pvector_change_priority(struct pvector *pvec, void *ptr, int priority)
|
2014-06-26 07:41:25 -07:00
|
|
|
{
|
2014-11-13 11:54:31 -08:00
|
|
|
struct pvector_impl *old = pvec->temp;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
if (!old) {
|
|
|
|
old = pvector_impl_get(pvec);
|
|
|
|
}
|
|
|
|
|
|
|
|
index = pvector_impl_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. */
|
|
|
|
if (!pvec->temp) {
|
|
|
|
/* Have to reallocate to reorder. */
|
|
|
|
pvec->temp = pvector_impl_dup(old);
|
|
|
|
old = pvec->temp;
|
|
|
|
/* 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. */
|
|
|
|
void pvector_publish__(struct pvector *pvec)
|
|
|
|
{
|
|
|
|
struct pvector_impl *temp = pvec->temp;
|
2014-06-26 07:41:25 -07:00
|
|
|
|
2014-11-13 11:54:31 -08:00
|
|
|
pvec->temp = NULL;
|
|
|
|
pvector_impl_sort(temp); /* Also removes gaps. */
|
|
|
|
ovsrcu_postpone(free, ovsrcu_get_protected(struct pvector_impl *,
|
|
|
|
&pvec->impl));
|
|
|
|
ovsrcu_set(&pvec->impl, temp);
|
2014-06-26 07:41:25 -07:00
|
|
|
}
|