⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fixstr.c

📁 在ecos 下mingui 的移植开发
💻 C
字号:
//// fixstr.c: the Fixed String module.// // Copyright (C) 1999, Wei Yongming.//// Current maintainer: Wei Yongming./***  This library is free software; you can redistribute it and/or**  modify it under the terms of the GNU Library General Public**  License as published by the Free Software Foundation; either**  version 2 of the License, or (at your option) any later version.****  This library is distributed in the hope that it will be useful,**  but WITHOUT ANY WARRANTY; without even the implied warranty of**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU**  Library General Public License for more details.****  You should have received a copy of the GNU Library General Public**  License along with this library; if not, write to the Free**  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,**  MA 02111-1307, USA*/// Create date: 1999/04/18////// Modify records:////  Who             When        Where       For What                Status//-----------------------------------------------------------------------------//// TODO:// #include <stdio.h>#include <stdlib.h>#include <string.h>#include <pthread.h>#include <semaphore.h>#include <errno.h>#include "common.h"#include "minigui.h"#ifndef lintstatic char fileid[] = "$Id: fixstr.c,v 1.3 2000/06/20 01:36:30 weiym Exp $";#endif/********************* functions may be used by other module *****************/BOOL InitFixStr(void);void TerminateFixStr(void);//****************** Module internal data ***********************************/struct FIXSTR {    BYTE bitmap[1+2+4+8+16+32+64+128+256+512];    int offset[10];        char* heap[10];    char* head;    char* tail;    pthread_mutex_t lock;}FixStrHeap;/****************** Module functions *****************************************/BOOL InitFixStr (void){    int i, j, offset;    BYTE* bitmap;        // allocate memory.    if (!(FixStrHeap.head = malloc (1024 * 160))) return FALSE;    for (i=0; i<10; i++) {        FixStrHeap.heap[i] = FixStrHeap.head + 1024*16*i;    }    FixStrHeap.tail = FixStrHeap.head + 1024*160;       // reset bitmap    bitmap = FixStrHeap.bitmap;    offset = 0;    for (i = 0; i < 10; i++) {        for (j = 0; j < (1<<i); j ++)            bitmap[j] |= 0xFF;        bitmap += 1<<i;                FixStrHeap.offset[i] = offset;                offset += 1<<i;    }        pthread_mutex_init (&FixStrHeap.lock, NULL);    return TRUE;}void TerminateFixStr (void){    free (FixStrHeap.head);    FixStrHeap.head = FixStrHeap.tail = NULL;}char* GUIAPI FixStrAlloc (int len){    UINT ulen = (UINT)len;    int i, j, btlen, bufflen;    char* heap;    BYTE* bitmap;    if (len < 0)        return NULL;            if (len == 0)        return "";            if (len >= 2048)        return (char*)malloc (len + 1);        // determine which heap will use.    i = 0;    while (ulen) {        ulen = ulen >> 1;        i++;    }    // if 2 > len >= 1, then i = 1.    // if 4 > len >= 2, then i = 2.    // if 8 > len >= 4, then i = 3;    // ...    // if 2K > len >= 1K, then i = 11;    if (i == 1) i = 2;    bufflen = 1 << i;        i = 11 - i;    // i is the heap index;    pthread_mutex_lock (&FixStrHeap.lock);    heap = FixStrHeap.heap[i];    bitmap = FixStrHeap.bitmap + FixStrHeap.offset[i];    btlen = 1 << i;        for (i=0; i<btlen; i++) {        for(j=0; j<8; j++) {            if ((*bitmap << j) & 0x80) {                *bitmap &= (~(0x80 >> j));                pthread_mutex_unlock (&FixStrHeap.lock);                return heap;            }            heap += bufflen;        }                bitmap++;    }    pthread_mutex_unlock (&FixStrHeap.lock);    return (char*)malloc (len + 1);}void GUIAPI FreeFixStr (char* str){    char* heap;    BYTE* bitmap;    int i;    UINT ulen;    int bufflen;    int stroff;        if (strlen (str) == 0)        return;            if (str > FixStrHeap.tail || str < FixStrHeap.head) {        free (str);        return;    }    // determine which heap used.    ulen = (UINT)strlen (str);    i = 0;    while (ulen) {        ulen = ulen >> 1;        i++;    }    if (i == 1) i = 2;    bufflen = 1 << i;        i = 11 - i;    // i is the heap index;    pthread_mutex_lock (&FixStrHeap.lock);    heap = FixStrHeap.heap[i];    bitmap = FixStrHeap.bitmap + FixStrHeap.offset[i];    // locate the bitmap and reset the bit.    stroff = 0;    while (str != heap) {        heap += bufflen;        stroff ++;    }    bitmap = bitmap + stroff/8;    *bitmap |= (0x80 >> (stroff%8));    pthread_mutex_unlock (&FixStrHeap.lock);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -