📄 dst_codeword_heap.c
字号:
/*****************************************************************************/
/* Copyright 1998, Hewlett-Packard Company */
/* All rights reserved */
/* File: "dst_codeword_heap.c" */
/* Description: Implementation of the `dst_codeword_heap' object to manage */
/* efficient dynamic resizing and recycling of storage for */
/* compressed code words produced by a coding engine. */
/* Author: David Taubman */
/* Affiliation: Hewlett-Packard and */
/* The University of New South Wales, Australia */
/* Version: VM7.1 */
/* Last Revised: 24 April, 2000 */
/*****************************************************************************/
/*****************************************************************************/
/* Modified to incorporate scan based rate control. Material identified by */
/* "CRIL Technology/SAIC Scan Buffer" comments has been added by Fabrice */
/* Pelleau and Olivier Duffez of CRIL Technology and Tom Flohr of SAIC. */
/* Copyright 2000 CRIL Technology */
/* Copyright 2000 CNES */
/* Copyright 2000 Science Applications International Corporation (SAIC). */
/* All Rights Reserved for modified parts. */
/*****************************************************************************/
#include <local_services.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ifc.h>
#include "dst_codeword_heap.h"
/*****************************************************************************/
/* STATIC __get_unit */
/*****************************************************************************/
static dst_heap_unit_ptr
__get_unit(dst_codeword_heap_ref self)
{
dst_heap_group_ptr scan;
dst_heap_unit_ptr unit;
int i;
for (scan=self->heap; scan != NULL; scan=scan->next)
if (scan->free_units)
break;
if (scan == NULL)
{
scan = (dst_heap_group_ptr)
local_malloc(DST_CODE_MEM_KEY,sizeof(dst_heap_group));
scan->next = self->heap;
self->heap = scan;
scan->free_units = DST_HEAP_GROUP_UNITS;
for (unit=scan->units, i=DST_HEAP_GROUP_UNITS; i > 0; i--, unit++)
{
unit->next = NULL;
unit->group = NULL;
}
}
for (unit=scan->units, i=DST_HEAP_GROUP_UNITS; i > 0; i--, unit++)
if (unit->group == NULL)
break;
assert(i > 0);
scan->free_units--;
unit->group = scan;
unit->next = NULL;
return(unit);
}
/*****************************************************************************/
/* STATIC __return_unit */
/*****************************************************************************/
static void
__return_unit(dst_codeword_heap_ref self, dst_heap_unit_ptr unit)
{
dst_heap_group_ptr group;
group = unit->group;
assert((group != NULL) && (group->free_units < DST_HEAP_GROUP_UNITS));
group->free_units++;
unit->group = NULL;
}
/* CRIL Technology/SAIC Scan Buffer begin */
/*****************************************************************************/
/* STATIC __return_units */
/*****************************************************************************/
static void
__return_units(dst_codeword_heap_ref self,
dst_heap_unit_ptr first_unit, int first_pos,
dst_heap_unit_ptr last_unit, int last_unit_used)
{
dst_heap_unit_ptr heap;
dst_heap_group_ptr group;
int i;
int free_first_unit = 1;
int free_last_unit = 1;
int last_unit_used_start, first_unit_used_end;
assert(first_pos < DST_HEAP_WORDS);
/* Note units used on boundaries of first/last heaps that can be released */
first_unit_used_end =
(first_unit == last_unit)?last_unit_used:DST_HEAP_WORDS;
for (i=first_pos; i<first_unit_used_end; i++) {
first_unit->released[i] = 1;
}
last_unit_used_start = (first_unit == last_unit)?first_pos:0;
for (i=last_unit_used_start; i<last_unit_used; i++) {
last_unit->released[i] = 1;
}
/* See if can release first_unit */
heap = first_unit;
for (i=0; i<DST_HEAP_WORDS; i++) {
if (!(heap->released[i])) {
free_first_unit = 0;
break;
}
}
if (free_first_unit) {
for (i=0; i<DST_HEAP_WORDS; i++) {
heap->released[i] = 0;
}
group = heap->group;
assert((group != NULL) && (group->free_units < DST_HEAP_GROUP_UNITS));
group->free_units++;
heap->group = NULL;
if (first_unit != last_unit)
heap = heap->next;
}
else if (first_unit != last_unit) {
heap = heap->next;
}
/* Release every thing inside boundaries */
while (heap != last_unit) {
group = heap->group;
assert((group != NULL) && (group->free_units < DST_HEAP_GROUP_UNITS));
group->free_units++;
heap->group = NULL;
heap = heap->next;
}
assert(heap == last_unit);
for (i=0; i<DST_HEAP_WORDS; i++) {
if ((!heap->released[i])) {
free_last_unit = 0;
break;
}
}
if (free_last_unit) {
for (i=0; i<DST_HEAP_WORDS; i++) {
heap->released[i] = 0;
}
group = heap->group;
assert((group != NULL) && (group->free_units < DST_HEAP_GROUP_UNITS));
group->free_units++;
heap->group = NULL;
}
}
/* CRIL Technology/SAIC Scan Buffer end */
/*****************************************************************************/
/* STATIC __terminate */
/*****************************************************************************/
static void
__terminate(dst_codeword_heap_ref self)
{
dst_heap_group_ptr tmp;
while ((tmp=self->heap) != NULL)
{
self->heap = tmp->next;
local_free(tmp);
}
local_free(self);
}
/*****************************************************************************/
/* EXTERN create_dst_codeword_heap */
/*****************************************************************************/
dst_codeword_heap_ref
create_dst_codeword_heap(void)
{
dst_codeword_heap_ref result;
result = (dst_codeword_heap_ref)
local_malloc(DST_CODE_MEM_KEY,sizeof(dst_codeword_heap_obj));
memset(result,0,sizeof(dst_codeword_heap_obj));
result->get_unit = __get_unit;
result->return_unit = __return_unit;
/* CRIL Technology/SAIC Scan Buffer begin */
result->return_units = __return_units;
/* CRIL Technology/SAIC Scan Buffer begin */
result->terminate = __terminate;
return(result);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -