Changeset e5d4294 in mainline for uspace/app/edit/sheet.c


Ignore:
Timestamp:
2009-09-16T21:31:46Z (15 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
57688fe2
Parents:
8c73012 (diff), 743e17b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge editor improvements from ~jsvoboda/helenos/edit.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/edit/sheet.c

    r8c73012 re5d4294  
    5454#include <adt/list.h>
    5555#include <align.h>
     56#include <macros.h>
    5657
    5758#include "sheet.h"
    5859
    5960enum {
    60         TAB_WIDTH = 8
     61        TAB_WIDTH       = 8,
     62
     63        /** Initial  of dat buffer in bytes */
     64        INITIAL_SIZE    = 32
    6165};
    6266
     
    6468int sheet_init(sheet_t *sh)
    6569{
    66         sh->dbuf_size = 65536;
     70        sh->dbuf_size = INITIAL_SIZE;
    6771        sh->text_size = 0;
    6872
     
    9599        link_t *link;
    96100        tag_t *tag;
     101        char *newp;
    97102
    98103        sz = str_size(str);
    99         if (sh->text_size + sz > sh->dbuf_size)
    100                 return ELIMIT;
     104        if (sh->text_size + sz > sh->dbuf_size) {
     105                /* Enlarge data buffer. */
     106                newp = realloc(sh->data, sh->dbuf_size * 2);
     107                if (newp == NULL)
     108                        return ELIMIT;
     109
     110                sh->data = newp;
     111                sh->dbuf_size = sh->dbuf_size * 2;
     112        }
    101113
    102114        ipp = sh->data + pos->b_off;
    103115
     116        /* Copy data. */
    104117        memmove(ipp + sz, ipp, sh->text_size - pos->b_off);
    105118        memcpy(ipp, str, sz);
    106119        sh->text_size += sz;
    107120
    108         /* Adjust tags */
     121        /* Adjust tags. */
    109122
    110123        link = sh->tags_head.next;
     
    139152        link_t *link;
    140153        tag_t *tag;
     154        char *newp;
     155        size_t shrink_size;
    141156
    142157        spp = sh->data + spos->b_off;
     
    146161        sh->text_size -= sz;
    147162
    148         /* Adjust tags */
     163        /* Adjust tags. */
    149164        link = sh->tags_head.next;
    150165        while (link != &sh->tags_head) {
     
    158173                link = link->next;
    159174        }
    160        
     175
     176        /* See if we should free up some memory. */
     177        shrink_size = max(sh->dbuf_size / 4, INITIAL_SIZE);
     178        if (sh->text_size <= shrink_size && sh->dbuf_size > INITIAL_SIZE) {
     179                /* Shrink data buffer. */
     180                newp = realloc(sh->data, shrink_size);
     181                if (newp == NULL) {
     182                        /* Failed to shrink buffer... no matter. */
     183                        return EOK;
     184                }
     185
     186                sh->data = newp;
     187                sh->dbuf_size = shrink_size;
     188        }
     189
    161190        return EOK;
    162191}
Note: See TracChangeset for help on using the changeset viewer.