+struct align_region_t
+{
+ int alignment;
+ /* start in memory, and size in bytes */
+ void* start;
+ int size;
+ /* layout within a region:
+ - num_elements bytes, 0: free, 1: used, 2: used, combines with next
+ - padding to alignment
+ - data section
+ - waste space
+
+ start_data points to the start of the data section
+ */
+ void* start_data;
+ /* number of free blocks sized "alignment" */
+ int free;
+ struct align_region_t *next;
+};
+
+static struct align_region_t* align_regions = 0;
+
+static struct align_region_t *allocate_region(int alignment, int num_elements)
+{
+ struct align_region_t *new_region;
+#ifdef CONFIG_DEBUG_MALLOC
+ printf("%s(old align_regions=%p, alignment=%u, num_elements=%u)\n",
+ __func__, align_regions, alignment, num_elements);
+#endif
+
+ new_region = malloc(sizeof(struct align_region_t));
+
+ if (!new_region)
+ return NULL;
+ new_region->alignment = alignment;
+ new_region->start = malloc((num_elements+1) * alignment + num_elements);
+ if (!new_region->start) {
+ free(new_region);
+ return NULL;
+ }
+ new_region->start_data = (void*)((u32)(new_region->start + num_elements + alignment - 1) & (~(alignment-1)));
+ new_region->size = num_elements * alignment;
+ new_region->free = num_elements;
+ new_region->next = align_regions;
+ memset(new_region->start, 0, num_elements);
+ align_regions = new_region;
+ return new_region;
+}
+
+
+static int free_aligned(void* addr)
+{
+ struct align_region_t *reg = align_regions;
+ while (reg != 0)
+ {
+ if ((addr >= reg->start_data) && (addr < reg->start_data + reg->size))
+ {
+ int i = (addr-reg->start_data)/reg->alignment;
+ while (((u8*)reg->start)[i]==2)
+ {
+ ((u8*)reg->start)[i++]=0;
+ reg->free++;
+ }
+ ((u8*)reg->start)[i]=0;
+ reg->free++;
+ return 1;
+ }
+ reg = reg->next;
+ }
+ return 0;
+}
+
+void *memalign(size_t align, size_t size)
+{
+ if (size == 0) return 0;
+ if (align_regions == 0) {
+ align_regions = malloc(sizeof(struct align_region_t));
+ if (align_regions == NULL)
+ return NULL;
+ memset(align_regions, 0, sizeof(struct align_region_t));
+ }
+ struct align_region_t *reg = align_regions;
+look_further:
+ while (reg != 0)
+ {
+ if ((reg->alignment == align) && (reg->free >= (size + align - 1)/align))
+ {
+#ifdef CONFIG_DEBUG_MALLOC
+ printf(" found memalign region. %x free, %x required\n", reg->free, (size + align - 1)/align);
+#endif
+ break;
+ }
+ reg = reg->next;
+ }
+ if (reg == 0)
+ {
+#ifdef CONFIG_DEBUG_MALLOC
+ printf(" need to allocate a new memalign region\n");
+#endif
+ /* get align regions */
+ reg = allocate_region(align, (size<1024)?(1024/align):(((size-1)/align)+1));
+#ifdef CONFIG_DEBUG_MALLOC
+ printf(" ... returned %p\n", align_regions);
+#endif
+ }
+ if (reg == 0) {
+ /* Nothing available. */
+ return (void *)NULL;
+ }
+
+ int i, count = 0, target = (size+align-1)/align;
+ for (i = 0; i < (reg->size/align); i++)
+ {
+ if (((u8*)reg->start)[i] == 0)
+ {
+ count++;
+ if (count == target) {
+ count = i+1-count;
+ for (i=0; i<target-1; i++)
+ {
+ ((u8*)reg->start)[count+i]=2;
+ }
+ ((u8*)reg->start)[count+target-1]=1;
+ reg->free -= target;
+ return reg->start_data+(align*count);
+ }
+ } else {
+ count = 0;
+ }
+ }
+ goto look_further; // end condition is once a new region is allocated - it always has enough space
+}
+