~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_dynrec.c

  • Committer: Patrick Galbraith
  • Date: 2008-07-24 16:57:40 UTC
  • mto: (202.2.4 rename-mysql-to-drizzle)
  • mto: This revision was merged to the branch mainline in revision 212.
  • Revision ID: patg@ishvara-20080724165740-x58yw6zs6d9o17lf
Most everything working with client rename
mysqlslap test still fails... can't connect to the server

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
#include "myisamdef.h"
27
27
 
28
 
#ifdef HAVE_SYS_TYPES
29
 
#include <sys/types.h>
30
 
#endif
31
 
#ifdef HAVE_SYS_MMAN_H
32
 
#include <sys/mman.h>
33
 
#endif
34
 
#include <drizzled/util/test.h>
35
 
 
36
28
/* Enough for comparing if number is zero */
37
29
static char zero_string[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
38
30
 
39
 
static int write_dynamic_record(MI_INFO *info,const unsigned char *record,
 
31
static int write_dynamic_record(MI_INFO *info,const uchar *record,
40
32
                                ulong reclength);
41
33
static int _mi_find_writepos(MI_INFO *info,ulong reclength,my_off_t *filepos,
42
34
                             ulong *length);
43
 
static int update_dynamic_record(MI_INFO *info,my_off_t filepos,unsigned char *record,
 
35
static int update_dynamic_record(MI_INFO *info,my_off_t filepos,uchar *record,
44
36
                                 ulong reclength);
45
37
static int delete_dynamic_record(MI_INFO *info,my_off_t filepos,
46
 
                                 uint32_t second_read);
47
 
static int _mi_cmp_buffer(File file, const unsigned char *buff, my_off_t filepos,
48
 
                          uint32_t length);
 
38
                                 uint second_read);
 
39
static int _mi_cmp_buffer(File file, const uchar *buff, my_off_t filepos,
 
40
                          uint length);
49
41
 
50
42
/* Play it safe; We have a small stack when using threads */
51
43
#undef my_alloca
52
44
#undef my_afree
53
 
#define my_alloca(A) malloc((A))
54
 
#define my_afree(A) free((A))
 
45
#define my_alloca(A) my_malloc((A),MYF(0))
 
46
#define my_afree(A) my_free((A),MYF(0))
55
47
 
56
48
        /* Interface function from MI_INFO */
57
49
 
69
61
    1  error.
70
62
*/
71
63
 
72
 
bool mi_dynmap_file(MI_INFO *info, my_off_t size)
 
64
my_bool mi_dynmap_file(MI_INFO *info, my_off_t size)
73
65
{
74
66
  if (size > (my_off_t) (~((size_t) 0)) - MEMMAP_EXTRA_MARGIN)
75
67
  {
83
75
      mapping. When swap space is not reserved one might get SIGSEGV
84
76
      upon a write if no physical memory is available.
85
77
  */
86
 
  info->s->file_map= (unsigned char*)
 
78
  info->s->file_map= (uchar*)
87
79
                  my_mmap(0, (size_t)(size + MEMMAP_EXTRA_MARGIN),
88
80
                          info->s->mode==O_RDONLY ? PROT_READ :
89
81
                          PROT_READ | PROT_WRITE,
90
82
                          MAP_SHARED | MAP_NORESERVE,
91
83
                          info->dfile, 0L);
92
 
  if (info->s->file_map == (unsigned char*) MAP_FAILED)
 
84
  if (info->s->file_map == (uchar*) MAP_FAILED)
93
85
  {
94
86
    info->s->file_map= NULL;
95
87
    return(1);
116
108
{
117
109
  if (info->s->file_map)
118
110
  {
119
 
    my_munmap((char*) info->s->file_map,
120
 
              (size_t) info->s->mmaped_length + MEMMAP_EXTRA_MARGIN);
 
111
    VOID(my_munmap((char*) info->s->file_map,
 
112
                   (size_t) info->s->mmaped_length + MEMMAP_EXTRA_MARGIN));
121
113
    mi_dynmap_file(info, size);
122
114
  }
123
115
}
139
131
    0  ok
140
132
*/
141
133
 
142
 
size_t mi_mmap_pread(MI_INFO *info, unsigned char *Buffer,
 
134
size_t mi_mmap_pread(MI_INFO *info, uchar *Buffer,
143
135
                    size_t Count, my_off_t offset, myf MyFlags)
144
136
{
145
137
  if (info->s->concurrent_insert)
170
162
 
171
163
        /* wrapper for my_pread in case if mmap isn't used */
172
164
 
173
 
size_t mi_nommap_pread(MI_INFO *info, unsigned char *Buffer,
 
165
size_t mi_nommap_pread(MI_INFO *info, uchar *Buffer,
174
166
                       size_t Count, my_off_t offset, myf MyFlags)
175
167
{
176
168
  return my_pread(info->dfile, Buffer, Count, offset, MyFlags);
193
185
    !=0  error.  In this case return error from pwrite
194
186
*/
195
187
 
196
 
size_t mi_mmap_pwrite(MI_INFO *info, const unsigned char *Buffer,
 
188
size_t mi_mmap_pwrite(MI_INFO *info, const uchar *Buffer,
197
189
                      size_t Count, my_off_t offset, myf MyFlags)
198
190
{
199
191
  if (info->s->concurrent_insert)
226
218
 
227
219
        /* wrapper for my_pwrite in case if mmap isn't used */
228
220
 
229
 
size_t mi_nommap_pwrite(MI_INFO *info, const unsigned char *Buffer,
 
221
size_t mi_nommap_pwrite(MI_INFO *info, const uchar *Buffer,
230
222
                      size_t Count, my_off_t offset, myf MyFlags)
231
223
{
232
224
  return my_pwrite(info->dfile, Buffer, Count, offset, MyFlags);
233
225
}
234
226
 
235
227
 
236
 
int _mi_write_dynamic_record(MI_INFO *info, const unsigned char *record)
 
228
int _mi_write_dynamic_record(MI_INFO *info, const uchar *record)
237
229
{
238
230
  ulong reclength=_mi_rec_pack(info,info->rec_buff,record);
239
231
  return (write_dynamic_record(info,info->rec_buff,reclength));
240
232
}
241
233
 
242
 
int _mi_update_dynamic_record(MI_INFO *info, my_off_t pos, const unsigned char *record)
 
234
int _mi_update_dynamic_record(MI_INFO *info, my_off_t pos, const uchar *record)
243
235
{
244
 
  uint32_t length=_mi_rec_pack(info,info->rec_buff,record);
 
236
  uint length=_mi_rec_pack(info,info->rec_buff,record);
245
237
  return (update_dynamic_record(info,pos,info->rec_buff,length));
246
238
}
247
239
 
248
 
int _mi_write_blob_record(MI_INFO *info, const unsigned char *record)
 
240
int _mi_write_blob_record(MI_INFO *info, const uchar *record)
249
241
{
250
 
  unsigned char *rec_buff;
 
242
  uchar *rec_buff;
251
243
  int error;
252
244
  ulong reclength,reclength2,extra;
253
245
 
262
254
    return -1;
263
255
  }
264
256
#endif
265
 
  if (!(rec_buff=(unsigned char*) my_alloca(reclength)))
 
257
  if (!(rec_buff=(uchar*) my_alloca(reclength)))
266
258
  {
267
259
    my_errno= HA_ERR_OUT_OF_MEM; /* purecov: inspected */
268
260
    return(-1);
277
269
}
278
270
 
279
271
 
280
 
int _mi_update_blob_record(MI_INFO *info, my_off_t pos, const unsigned char *record)
 
272
int _mi_update_blob_record(MI_INFO *info, my_off_t pos, const uchar *record)
281
273
{
282
 
  unsigned char *rec_buff;
 
274
  uchar *rec_buff;
283
275
  int error;
284
276
  ulong reclength,extra;
285
277
 
294
286
    return -1;
295
287
  }
296
288
#endif
297
 
  if (!(rec_buff=(unsigned char*) my_alloca(reclength)))
 
289
  if (!(rec_buff=(uchar*) my_alloca(reclength)))
298
290
  {
299
291
    my_errno= HA_ERR_OUT_OF_MEM; /* purecov: inspected */
300
292
    return(-1);
317
309
 
318
310
        /* Write record to data-file */
319
311
 
320
 
static int write_dynamic_record(MI_INFO *info, const unsigned char *record,
 
312
static int write_dynamic_record(MI_INFO *info, const uchar *record,
321
313
                                ulong reclength)
322
314
{
323
315
  int flag;
355
347
    if (_mi_write_part_record(info,filepos,length,
356
348
                              (info->append_insert_at_end ?
357
349
                               HA_OFFSET_ERROR : info->s->state.dellink),
358
 
                              (unsigned char**) &record,&reclength,&flag))
 
350
                              (uchar**) &record,&reclength,&flag))
359
351
      goto err;
360
352
  } while (reclength);
361
353
 
427
419
  a big block.
428
420
*/
429
421
 
430
 
static bool unlink_deleted_block(MI_INFO *info, MI_BLOCK_INFO *block_info)
 
422
static my_bool unlink_deleted_block(MI_INFO *info, MI_BLOCK_INFO *block_info)
431
423
{
432
424
  if (block_info->filepos == info->s->state.dellink)
433
425
  {
501
493
    if (_mi_get_block_info(&block_info,info->dfile,delete_block)
502
494
        & BLOCK_DELETED)
503
495
    {
504
 
      unsigned char buff[8];
 
496
      uchar buff[8];
505
497
      mi_sizestore(buff,filepos);
506
498
      if (info->s->file_write(info,buff, 8, delete_block+12, MYF(MY_NABP)))
507
499
        return(1);                              /* Error on write */
519
511
        /* info->rec_cache.seek_not_done is updated in cmp_record */
520
512
 
521
513
static int delete_dynamic_record(MI_INFO *info, my_off_t filepos,
522
 
                                 uint32_t second_read)
 
514
                                 uint second_read)
523
515
{
524
 
  uint32_t length,b_type;
 
516
  uint length,b_type;
525
517
  MI_BLOCK_INFO block_info,del_block;
526
518
  int error;
527
 
  bool remove_next_block;
 
519
  my_bool remove_next_block;
528
520
 
529
521
  /* First add a link from the last block to the new one */
530
522
  error= update_backward_delete_link(info, info->s->state.dellink, filepos);
557
549
    mi_int3store(block_info.header+1,length);
558
550
    mi_sizestore(block_info.header+4,info->s->state.dellink);
559
551
    if (b_type & BLOCK_LAST)
560
 
      memset(block_info.header+12, 255, 8);
 
552
      bfill(block_info.header+12,8,255);
561
553
    else
562
554
      mi_sizestore(block_info.header+12,block_info.next_filepos);
563
 
    if (info->s->file_write(info,(unsigned char*) block_info.header,20,filepos,
 
555
    if (info->s->file_write(info,(uchar*) block_info.header,20,filepos,
564
556
                  MYF(MY_NABP)))
565
557
      return(1);
566
558
    info->s->state.dellink = filepos;
583
575
                          my_off_t filepos,     /* points at empty block */
584
576
                          ulong length,         /* length of block */
585
577
                          my_off_t next_filepos,/* Next empty block */
586
 
                          unsigned char **record,       /* pointer to record ptr */
 
578
                          uchar **record,       /* pointer to record ptr */
587
579
                          ulong *reclength,     /* length of *record */
588
580
                          int *flag)            /* *flag == 0 if header */
589
581
{
590
582
  ulong head_length,res_length,extra_length,long_block,del_length;
591
 
  unsigned char *pos,*record_end;
 
583
  uchar *pos,*record_end;
592
584
  my_off_t  next_delete_block;
593
 
  unsigned char temp[MI_SPLIT_LENGTH+MI_DYN_DELETE_BLOCK_HEADER];
 
585
  uchar temp[MI_SPLIT_LENGTH+MI_DYN_DELETE_BLOCK_HEADER];
594
586
 
595
587
  next_delete_block=HA_OFFSET_ERROR;
596
588
 
605
597
  if (length == *reclength+ 3 + long_block)
606
598
  {
607
599
    /* Block is exactly of the right length */
608
 
    temp[0]=(unsigned char) (1+ *flag)+(unsigned char) long_block;      /* Flag is 0 or 6 */
 
600
    temp[0]=(uchar) (1+ *flag)+(uchar) long_block;      /* Flag is 0 or 6 */
609
601
    if (long_block)
610
602
    {
611
603
      mi_int3store(temp+1,*reclength);
631
623
        temp[0]=13;
632
624
        mi_int4store(temp+1,*reclength);
633
625
        mi_int3store(temp+5,length-head_length);
634
 
        mi_sizestore((unsigned char*) temp+8,next_filepos);
 
626
        mi_sizestore((uchar*) temp+8,next_filepos);
635
627
      }
636
628
      else
637
629
      {
638
630
        head_length=5+8+long_block*2;
639
 
        temp[0]=5+(unsigned char) long_block;
 
631
        temp[0]=5+(uchar) long_block;
640
632
        if (long_block)
641
633
        {
642
634
          mi_int3store(temp+1,*reclength);
643
635
          mi_int3store(temp+4,length-head_length);
644
 
          mi_sizestore((unsigned char*) temp+7,next_filepos);
 
636
          mi_sizestore((uchar*) temp+7,next_filepos);
645
637
        }
646
638
        else
647
639
        {
648
640
          mi_int2store(temp+1,*reclength);
649
641
          mi_int2store(temp+3,length-head_length);
650
 
          mi_sizestore((unsigned char*) temp+5,next_filepos);
 
642
          mi_sizestore((uchar*) temp+5,next_filepos);
651
643
        }
652
644
      }
653
645
    }
654
646
    else
655
647
    {
656
648
      head_length=3+8+long_block;
657
 
      temp[0]=11+(unsigned char) long_block;
 
649
      temp[0]=11+(uchar) long_block;
658
650
      if (long_block)
659
651
      {
660
652
        mi_int3store(temp+1,length-head_length);
661
 
        mi_sizestore((unsigned char*) temp+4,next_filepos);
 
653
        mi_sizestore((uchar*) temp+4,next_filepos);
662
654
      }
663
655
      else
664
656
      {
665
657
        mi_int2store(temp+1,length-head_length);
666
 
        mi_sizestore((unsigned char*) temp+3,next_filepos);
 
658
        mi_sizestore((uchar*) temp+3,next_filepos);
667
659
      }
668
660
    }
669
661
  }
671
663
  {                                     /* Block with empty info last */
672
664
    head_length=4+long_block;
673
665
    extra_length= length- *reclength-head_length;
674
 
    temp[0]= (unsigned char) (3+ *flag)+(unsigned char) long_block; /* 3,4 or 9,10 */
 
666
    temp[0]= (uchar) (3+ *flag)+(uchar) long_block; /* 3,4 or 9,10 */
675
667
    if (long_block)
676
668
    {
677
669
      mi_int3store(temp+1,*reclength);
678
 
      temp[4]= (unsigned char) (extra_length);
 
670
      temp[4]= (uchar) (extra_length);
679
671
    }
680
672
    else
681
673
    {
682
674
      mi_int2store(temp+1,*reclength);
683
 
      temp[3]= (unsigned char) (extra_length);
 
675
      temp[3]= (uchar) (extra_length);
684
676
    }
685
677
    length=       *reclength+head_length;       /* Write only what is needed */
686
678
  }
688
680
        /* Make a long block for one write */
689
681
  record_end= *record+length-head_length;
690
682
  del_length=(res_length ? MI_DYN_DELETE_BLOCK_HEADER : 0);
691
 
  memcpy(*record - head_length, temp, head_length);
 
683
  bmove((uchar*) (*record-head_length),(uchar*) temp,head_length);
692
684
  memcpy(temp,record_end,(size_t) (extra_length+del_length));
693
 
  memset(record_end, 0, extra_length);
 
685
  bzero((uchar*) record_end,extra_length);
694
686
 
695
687
  if (res_length)
696
688
  {
717
709
    pos[0]= '\0';
718
710
    mi_int3store(pos+1,res_length);
719
711
    mi_sizestore(pos+4,info->s->state.dellink);
720
 
    memset(pos+12, 255, 8);                     /* End link */
 
712
    bfill(pos+12,8,255);                        /* End link */
721
713
    next_delete_block=info->s->state.dellink;
722
714
    info->s->state.dellink= filepos+length+extra_length;
723
715
    info->state->del++;
730
722
    if (info->update & HA_STATE_EXTEND_BLOCK)
731
723
    {
732
724
      info->update&= ~HA_STATE_EXTEND_BLOCK;
733
 
      if (my_block_write(&info->rec_cache,(unsigned char*) *record-head_length,
 
725
      if (my_block_write(&info->rec_cache,(uchar*) *record-head_length,
734
726
                         length+extra_length+del_length,filepos))
735
727
      goto err;
736
728
    }
737
 
    else if (my_b_write(&info->rec_cache,(unsigned char*) *record-head_length,
 
729
    else if (my_b_write(&info->rec_cache,(uchar*) *record-head_length,
738
730
                        length+extra_length+del_length))
739
731
      goto err;
740
732
  }
741
733
  else
742
734
  {
743
735
    info->rec_cache.seek_not_done=1;
744
 
    if (info->s->file_write(info,(unsigned char*) *record-head_length,length+extra_length+
 
736
    if (info->s->file_write(info,(uchar*) *record-head_length,length+extra_length+
745
737
                  del_length,filepos,info->s->write_flag))
746
738
      goto err;
747
739
  }
748
 
  memcpy(record_end, temp, extra_length + del_length);
 
740
  memcpy(record_end,temp,(size_t) (extra_length+del_length));
749
741
  *record=record_end;
750
742
  *reclength-=(length-head_length);
751
743
  *flag=6;
766
758
 
767
759
        /* update record from datafile */
768
760
 
769
 
static int update_dynamic_record(MI_INFO *info, my_off_t filepos, unsigned char *record,
 
761
static int update_dynamic_record(MI_INFO *info, my_off_t filepos, uchar *record,
770
762
                                 ulong reclength)
771
763
{
772
764
  int flag;
773
 
  uint32_t error;
 
765
  uint error;
774
766
  ulong length;
775
767
  MI_BLOCK_INFO block_info;
776
768
 
835
827
      length=(ulong) (block_info.filepos-filepos) + block_info.block_len;
836
828
      if (length < reclength)
837
829
      {
838
 
        uint32_t tmp=MY_ALIGN(reclength - length + 3 +
 
830
        uint tmp=MY_ALIGN(reclength - length + 3 +
839
831
                          test(reclength >= 65520L),MI_DYN_ALIGN_SIZE);
840
832
        /* Don't create a block bigger than MI_MAX_BLOCK_LENGTH */
841
 
        tmp= cmin(length+tmp, MI_MAX_BLOCK_LENGTH)-length;
 
833
        tmp= min(length+tmp, MI_MAX_BLOCK_LENGTH)-length;
842
834
        /* Check if we can extend this block */
843
835
        if (block_info.filepos + block_info.block_len ==
844
836
            info->state->data_file_length &&
889
881
              del_block.header[0]=0;
890
882
              mi_int3store(del_block.header+1, rest_length);
891
883
              mi_sizestore(del_block.header+4,info->s->state.dellink);
892
 
              memset(del_block.header+12, 255, 8);
893
 
              if (info->s->file_write(info,(unsigned char*) del_block.header,20, next_pos,
 
884
              bfill(del_block.header+12,8,255);
 
885
              if (info->s->file_write(info,(uchar*) del_block.header,20, next_pos,
894
886
                            MYF(MY_NABP)))
895
887
                return(1);
896
888
              info->s->state.dellink= next_pos;
929
921
 
930
922
        /* Pack a record. Return new reclength */
931
923
 
932
 
uint32_t _mi_rec_pack(MI_INFO *info, register unsigned char *to,
933
 
                  register const unsigned char *from)
 
924
uint _mi_rec_pack(MI_INFO *info, register uchar *to,
 
925
                  register const uchar *from)
934
926
{
935
927
  uint          length,new_length,flag,bit,i;
936
 
  unsigned char         *pos,*end,*startpos,*packpos;
 
928
  uchar         *pos,*end,*startpos,*packpos;
937
929
  enum en_fieldtype type;
938
930
  register MI_COLUMNDEF *rec;
939
931
  MI_BLOB       *blob;
955
947
        {
956
948
          char *temp_pos;
957
949
          size_t tmp_length=length-portable_sizeof_char_ptr;
958
 
          memcpy(to,from,tmp_length);
959
 
          memcpy(&temp_pos,from+tmp_length,sizeof(char*));
960
 
          memcpy(to + tmp_length, temp_pos, blob->length);
 
950
          memcpy((uchar*) to,from,tmp_length);
 
951
          memcpy_fixed(&temp_pos,from+tmp_length,sizeof(char*));
 
952
          memcpy(to+tmp_length,temp_pos,(size_t) blob->length);
961
953
          to+=tmp_length+blob->length;
962
954
        }
963
955
        blob++;
964
956
      }
965
957
      else if (type == FIELD_SKIP_ZERO)
966
958
      {
967
 
        if (memcmp(from,zero_string,length) == 0)
 
959
        if (memcmp((uchar*) from,zero_string,length) == 0)
968
960
          flag|=bit;
969
961
        else
970
962
        {
971
 
          memcpy(to, from, length);
972
 
          to+=length;
 
963
          memcpy((uchar*) to,from,(size_t) length); to+=length;
973
964
        }
974
965
      }
975
966
      else if (type == FIELD_SKIP_ENDSPACE ||
976
967
               type == FIELD_SKIP_PRESPACE)
977
968
      {
978
 
        pos= (unsigned char*) from; end= (unsigned char*) from + length;
 
969
        pos= (uchar*) from; end= (uchar*) from + length;
979
970
        if (type == FIELD_SKIP_ENDSPACE)
980
971
        {                                       /* Pack trailing spaces */
981
972
          while (end > from && *(end-1) == ' ')
992
983
        {
993
984
          if (rec->length > 255 && new_length > 127)
994
985
          {
995
 
            to[0]= (unsigned char) ((new_length & 127) + 128);
996
 
            to[1]= (unsigned char) (new_length >> 7);
 
986
            to[0]= (uchar) ((new_length & 127) + 128);
 
987
            to[1]= (uchar) (new_length >> 7);
997
988
            to+=2;
998
989
          }
999
990
          else
1000
 
            *to++= (unsigned char) new_length;
1001
 
          memcpy(to, pos, new_length);
1002
 
          to+=new_length;
 
991
            *to++= (uchar) new_length;
 
992
          memcpy((uchar*) to,pos,(size_t) new_length); to+=new_length;
1003
993
          flag|=bit;
1004
994
        }
1005
995
        else
1006
996
        {
1007
 
          memcpy(to, from, length);
1008
 
          to+=length;
 
997
          memcpy(to,from,(size_t) length); to+=length;
1009
998
        }
1010
999
      }
1011
1000
      else if (type == FIELD_VARCHAR)
1012
1001
      {
1013
 
        uint32_t pack_length= HA_VARCHAR_PACKLENGTH(rec->length -1);
1014
 
        uint32_t tmp_length;
 
1002
        uint pack_length= HA_VARCHAR_PACKLENGTH(rec->length -1);
 
1003
        uint tmp_length;
1015
1004
        if (pack_length == 1)
1016
1005
        {
1017
 
          tmp_length= (uint) *(unsigned char*) from;
 
1006
          tmp_length= (uint) *(uchar*) from;
1018
1007
          *to++= *from;
1019
1008
        }
1020
1009
        else
1022
1011
          tmp_length= uint2korr(from);
1023
1012
          store_key_length_inc(to,tmp_length);
1024
1013
        }
1025
 
        memcpy(to, from+pack_length, tmp_length);
 
1014
        memcpy(to, from+pack_length,tmp_length);
1026
1015
        to+= tmp_length;
1027
1016
        continue;
1028
1017
      }
1029
1018
      else
1030
1019
      {
1031
 
        memcpy(to, from, length);
1032
 
        to+=length;
 
1020
        memcpy(to,from,(size_t) length); to+=length;
1033
1021
        continue;                               /* Normal field */
1034
1022
      }
1035
1023
      if ((bit= bit << 1) >= 256)
1036
1024
      {
1037
 
        *packpos++= (unsigned char) flag;
 
1025
        *packpos++= (uchar) flag;
1038
1026
        bit=1; flag=0;
1039
1027
      }
1040
1028
    }
1041
1029
    else
1042
1030
    {
1043
 
      memcpy(to, from, length);
1044
 
      to+=length;
 
1031
      memcpy(to,from,(size_t) length); to+=length;
1045
1032
    }
1046
1033
  }
1047
1034
  if (bit != 1)
1048
 
    *packpos= (unsigned char) flag;
 
1035
    *packpos= (uchar) flag;
1049
1036
  if (info->s->calc_checksum)
1050
 
    *to++= (unsigned char) info->checksum;
 
1037
    *to++= (uchar) info->checksum;
1051
1038
  return((uint) (to-startpos));
1052
1039
} /* _mi_rec_pack */
1053
1040
 
1058
1045
  Returns 0 if record is ok.
1059
1046
*/
1060
1047
 
1061
 
bool _mi_rec_check(MI_INFO *info,const unsigned char *record, unsigned char *rec_buff,
1062
 
                      ulong packed_length, bool with_checksum)
 
1048
my_bool _mi_rec_check(MI_INFO *info,const uchar *record, uchar *rec_buff,
 
1049
                      ulong packed_length, my_bool with_checksum)
1063
1050
{
1064
1051
  uint          length,new_length,flag,bit,i;
1065
 
  unsigned char         *pos,*end,*packpos,*to;
 
1052
  uchar         *pos,*end,*packpos,*to;
1066
1053
  enum en_fieldtype type;
1067
1054
  register MI_COLUMNDEF *rec;
1068
1055
 
1077
1064
    {
1078
1065
      if (type == FIELD_BLOB)
1079
1066
      {
1080
 
        uint32_t blob_length=
 
1067
        uint blob_length=
1081
1068
          _mi_calc_blob_length(length-portable_sizeof_char_ptr,record);
1082
1069
        if (!blob_length && !(flag & bit))
1083
1070
          goto err;
1086
1073
      }
1087
1074
      else if (type == FIELD_SKIP_ZERO)
1088
1075
      {
1089
 
        if (memcmp(record,zero_string,length) == 0)
 
1076
        if (memcmp((uchar*) record,zero_string,length) == 0)
1090
1077
        {
1091
1078
          if (!(flag & bit))
1092
1079
            goto err;
1097
1084
      else if (type == FIELD_SKIP_ENDSPACE ||
1098
1085
               type == FIELD_SKIP_PRESPACE)
1099
1086
      {
1100
 
        pos= (unsigned char*) record; end= (unsigned char*) record + length;
 
1087
        pos= (uchar*) record; end= (uchar*) record + length;
1101
1088
        if (type == FIELD_SKIP_ENDSPACE)
1102
1089
        {                                       /* Pack trailing spaces */
1103
1090
          while (end > record && *(end-1) == ' ')
1117
1104
          if (rec->length > 255 && new_length > 127)
1118
1105
          {
1119
1106
            /* purecov: begin inspected */
1120
 
            if (to[0] != (unsigned char) ((new_length & 127) + 128) ||
1121
 
                to[1] != (unsigned char) (new_length >> 7))
 
1107
            if (to[0] != (uchar) ((new_length & 127) + 128) ||
 
1108
                to[1] != (uchar) (new_length >> 7))
1122
1109
              goto err;
1123
1110
            to+=2;
1124
1111
            /* purecov: end */
1125
1112
          }
1126
 
          else if (*to++ != (unsigned char) new_length)
 
1113
          else if (*to++ != (uchar) new_length)
1127
1114
            goto err;
1128
1115
          to+=new_length;
1129
1116
        }
1132
1119
      }
1133
1120
      else if (type == FIELD_VARCHAR)
1134
1121
      {
1135
 
        uint32_t pack_length= HA_VARCHAR_PACKLENGTH(rec->length -1);
1136
 
        uint32_t tmp_length;
 
1122
        uint pack_length= HA_VARCHAR_PACKLENGTH(rec->length -1);
 
1123
        uint tmp_length;
1137
1124
        if (pack_length == 1)
1138
1125
        {
1139
 
          tmp_length= (uint) *(unsigned char*) record;
 
1126
          tmp_length= (uint) *(uchar*) record;
1140
1127
          to+= 1+ tmp_length;
1141
1128
          continue;
1142
1129
        }
1164
1151
  if (packed_length != (uint) (to - rec_buff) + test(info->s->calc_checksum) ||
1165
1152
      (bit != 1 && (flag & ~(bit - 1))))
1166
1153
    goto err;
1167
 
  if (with_checksum && ((unsigned char) info->checksum != (unsigned char) *to))
 
1154
  if (with_checksum && ((uchar) info->checksum != (uchar) *to))
1168
1155
  {
1169
1156
    goto err;
1170
1157
  }
1180
1167
        /* Returns -1 and my_errno =HA_ERR_RECORD_DELETED if reclength isn't */
1181
1168
        /* right. Returns reclength (>0) if ok */
1182
1169
 
1183
 
ulong _mi_rec_unpack(register MI_INFO *info, register unsigned char *to, unsigned char *from,
 
1170
ulong _mi_rec_unpack(register MI_INFO *info, register uchar *to, uchar *from,
1184
1171
                     ulong found_length)
1185
1172
{
1186
 
  uint32_t flag,bit,length,rec_length,min_pack_length;
 
1173
  uint flag,bit,length,rec_length,min_pack_length;
1187
1174
  enum en_fieldtype type;
1188
 
  unsigned char *from_end,*to_end,*packpos;
 
1175
  uchar *from_end,*to_end,*packpos;
1189
1176
  register MI_COLUMNDEF *rec,*end_field;
1190
1177
 
1191
1178
  to_end=to + info->s->base.reclength;
1192
1179
  from_end=from+found_length;
1193
 
  flag= (unsigned char) *from; bit=1; packpos=from;
 
1180
  flag= (uchar) *from; bit=1; packpos=from;
1194
1181
  if (found_length < info->s->base.min_pack_length)
1195
1182
    goto err;
1196
1183
  from+= info->s->base.pack_bits;
1205
1192
    {
1206
1193
      if (type == FIELD_VARCHAR)
1207
1194
      {
1208
 
        uint32_t pack_length= HA_VARCHAR_PACKLENGTH(rec_length-1);
 
1195
        uint pack_length= HA_VARCHAR_PACKLENGTH(rec_length-1);
1209
1196
        if (pack_length == 1)
1210
1197
        {
1211
 
          length= (uint) *(unsigned char*) from;
 
1198
          length= (uint) *(uchar*) from;
1212
1199
          if (length > rec_length-1)
1213
1200
            goto err;
1214
1201
          *to= *from++;
1230
1217
      if (flag & bit)
1231
1218
      {
1232
1219
        if (type == FIELD_BLOB || type == FIELD_SKIP_ZERO)
1233
 
          memset(to, 0, rec_length);
 
1220
          bzero((uchar*) to,rec_length);
1234
1221
        else if (type == FIELD_SKIP_ENDSPACE ||
1235
1222
                 type == FIELD_SKIP_PRESPACE)
1236
1223
        {
1238
1225
          {
1239
1226
            if (from + 1 >= from_end)
1240
1227
              goto err;
1241
 
            length= (*from & 127)+ ((uint) (unsigned char) *(from+1) << 7); from+=2;
 
1228
            length= (*from & 127)+ ((uint) (uchar) *(from+1) << 7); from+=2;
1242
1229
          }
1243
1230
          else
1244
1231
          {
1245
1232
            if (from == from_end)
1246
1233
              goto err;
1247
 
            length= (unsigned char) *from++;
 
1234
            length= (uchar) *from++;
1248
1235
          }
1249
1236
          min_pack_length--;
1250
1237
          if (length >= rec_length ||
1252
1239
            goto err;
1253
1240
          if (type == FIELD_SKIP_ENDSPACE)
1254
1241
          {
1255
 
            memcpy(to, from, length);
1256
 
            memset(to+length, ' ', rec_length-length);
 
1242
            memcpy(to,(uchar*) from,(size_t) length);
 
1243
            bfill((uchar*) to+length,rec_length-length,' ');
1257
1244
          }
1258
1245
          else
1259
1246
          {
1260
 
            memset(to, ' ', rec_length-length);
1261
 
            memcpy(to + rec_length - length, from, length);
 
1247
            bfill((uchar*) to,rec_length-length,' ');
 
1248
            memcpy(to+rec_length-length,(uchar*) from,(size_t) length);
1262
1249
          }
1263
1250
          from+=length;
1264
1251
        }
1265
1252
      }
1266
1253
      else if (type == FIELD_BLOB)
1267
1254
      {
1268
 
        uint32_t size_length=rec_length- portable_sizeof_char_ptr;
 
1255
        uint size_length=rec_length- portable_sizeof_char_ptr;
1269
1256
        ulong blob_length=_mi_calc_blob_length(size_length,from);
1270
1257
        ulong from_left= (ulong) (from_end - from);
1271
1258
        if (from_left < size_length ||
1272
1259
            from_left - size_length < blob_length ||
1273
1260
            from_left - size_length - blob_length < min_pack_length)
1274
1261
          goto err;
1275
 
        memcpy(to, from, size_length);
 
1262
        memcpy((uchar*) to,(uchar*) from,(size_t) size_length);
1276
1263
        from+=size_length;
1277
 
        memcpy(to+size_length, &from, sizeof(char*));
 
1264
        memcpy_fixed((uchar*) to+size_length,(uchar*) &from,sizeof(char*));
1278
1265
        from+=blob_length;
1279
1266
      }
1280
1267
      else
1283
1270
          min_pack_length--;
1284
1271
        if (min_pack_length + rec_length > (uint) (from_end - from))
1285
1272
          goto err;
1286
 
        memcpy(to, from, rec_length);
1287
 
        from+=rec_length;
 
1273
        memcpy(to,(uchar*) from,(size_t) rec_length); from+=rec_length;
1288
1274
      }
1289
1275
      if ((bit= bit << 1) >= 256)
1290
1276
      {
1291
 
        flag= (unsigned char) *++packpos; bit=1;
 
1277
        flag= (uchar) *++packpos; bit=1;
1292
1278
      }
1293
1279
    }
1294
1280
    else
1296
1282
      if (min_pack_length > (uint) (from_end - from))
1297
1283
        goto err;
1298
1284
      min_pack_length-=rec_length;
1299
 
      memcpy(to, from, rec_length);
 
1285
      memcpy(to, (uchar*) from, (size_t) rec_length);
1300
1286
      from+=rec_length;
1301
1287
    }
1302
1288
  }
1313
1299
 
1314
1300
        /* Calc length of blob. Update info in blobs->length */
1315
1301
 
1316
 
ulong _my_calc_total_blob_length(MI_INFO *info, const unsigned char *record)
 
1302
ulong _my_calc_total_blob_length(MI_INFO *info, const uchar *record)
1317
1303
{
1318
1304
  ulong length;
1319
1305
  MI_BLOB *blob,*end;
1329
1315
}
1330
1316
 
1331
1317
 
1332
 
ulong _mi_calc_blob_length(uint32_t length, const unsigned char *pos)
 
1318
ulong _mi_calc_blob_length(uint length, const uchar *pos)
1333
1319
{
1334
1320
  switch (length) {
1335
1321
  case 1:
1336
 
    return (uint) (unsigned char) *pos;
 
1322
    return (uint) (uchar) *pos;
1337
1323
  case 2:
1338
1324
    return (uint) uint2korr(pos);
1339
1325
  case 3:
1347
1333
}
1348
1334
 
1349
1335
 
1350
 
void _my_store_blob_length(unsigned char *pos,uint32_t pack_length,uint32_t length)
 
1336
void _my_store_blob_length(uchar *pos,uint pack_length,uint length)
1351
1337
{
1352
1338
  switch (pack_length) {
1353
1339
  case 1:
1354
 
    *pos= (unsigned char) length;
 
1340
    *pos= (uchar) length;
1355
1341
    break;
1356
1342
  case 2:
1357
1343
    int2store(pos,length);
1400
1386
    -1          Error
1401
1387
*/
1402
1388
 
1403
 
int _mi_read_dynamic_record(MI_INFO *info, my_off_t filepos, unsigned char *buf)
 
1389
int _mi_read_dynamic_record(MI_INFO *info, my_off_t filepos, uchar *buf)
1404
1390
{
1405
1391
  int block_of_record;
1406
 
  uint32_t b_type, left_length= 0;
1407
 
  unsigned char *to= NULL;
 
1392
  uint b_type, left_length= 0;
 
1393
  uchar *to= NULL;
1408
1394
  MI_BLOCK_INFO block_info;
1409
1395
  File file;
1410
1396
 
1449
1435
        goto panic;                     /* Wrong linked record */
1450
1436
      /* copy information that is already read */
1451
1437
      {
1452
 
        uint32_t offset= (uint) (block_info.filepos - filepos);
1453
 
        uint32_t prefetch_len= (sizeof(block_info.header) - offset);
 
1438
        uint offset= (uint) (block_info.filepos - filepos);
 
1439
        uint prefetch_len= (sizeof(block_info.header) - offset);
1454
1440
        filepos+= sizeof(block_info.header);
1455
1441
 
1456
1442
        if (prefetch_len > block_info.data_len)
1457
1443
          prefetch_len= block_info.data_len;
1458
1444
        if (prefetch_len)
1459
1445
        {
1460
 
          memcpy(to, block_info.header + offset, prefetch_len);
 
1446
          memcpy((uchar*) to, block_info.header + offset, prefetch_len);
1461
1447
          block_info.data_len-= prefetch_len;
1462
1448
          left_length-= prefetch_len;
1463
1449
          to+= prefetch_len;
1475
1461
          there is no equivalent without seeking. We are at the right
1476
1462
          position already. :(
1477
1463
        */
1478
 
        if (info->s->file_read(info, (unsigned char*) to, block_info.data_len,
 
1464
        if (info->s->file_read(info, (uchar*) to, block_info.data_len,
1479
1465
                               filepos, MYF(MY_NABP)))
1480
1466
          goto panic;
1481
1467
        left_length-=block_info.data_len;
1495
1481
panic:
1496
1482
  my_errno=HA_ERR_WRONG_IN_RECORD;
1497
1483
err:
1498
 
  _mi_writeinfo(info,0);
 
1484
  VOID(_mi_writeinfo(info,0));
1499
1485
  return(-1);
1500
1486
}
1501
1487
 
1502
1488
        /* compare unique constraint between stored rows */
1503
1489
 
1504
1490
int _mi_cmp_dynamic_unique(MI_INFO *info, MI_UNIQUEDEF *def,
1505
 
                           const unsigned char *record, my_off_t pos)
 
1491
                           const uchar *record, my_off_t pos)
1506
1492
{
1507
 
  unsigned char *rec_buff,*old_record;
 
1493
  uchar *rec_buff,*old_record;
1508
1494
  int error;
1509
1495
 
1510
1496
  if (!(old_record=my_alloca(info->s->base.reclength)))
1519
1505
    error=mi_unique_comp(def, record, old_record, def->null_are_equal);
1520
1506
  if (info->s->base.blobs)
1521
1507
  {
1522
 
    void * rec_buff_ptr= mi_get_rec_buff_ptr(info, info->rec_buff);
1523
 
    if (rec_buff_ptr != NULL)
1524
 
      free(rec_buff_ptr);
 
1508
    my_free(mi_get_rec_buff_ptr(info, info->rec_buff), MYF(MY_ALLOW_ZERO_PTR));
1525
1509
    info->rec_buff=rec_buff;
1526
1510
  }
1527
1511
  my_afree(old_record);
1531
1515
 
1532
1516
        /* Compare of record one disk with packed record in memory */
1533
1517
 
1534
 
int _mi_cmp_dynamic_record(register MI_INFO *info, register const unsigned char *record)
 
1518
int _mi_cmp_dynamic_record(register MI_INFO *info, register const uchar *record)
1535
1519
{
1536
 
  uint32_t flag,reclength,b_type;
 
1520
  uint flag,reclength,b_type;
1537
1521
  my_off_t filepos;
1538
 
  unsigned char *buffer;
 
1522
  uchar *buffer;
1539
1523
  MI_BLOCK_INFO block_info;
1540
1524
 
1541
1525
  if (info->opt_flag & WRITE_CACHE_USED)
1553
1537
  {                                             /* If check isn't disabled  */
1554
1538
    if (info->s->base.blobs)
1555
1539
    {
1556
 
      if (!(buffer=(unsigned char*) my_alloca(info->s->base.pack_reclength+
 
1540
      if (!(buffer=(uchar*) my_alloca(info->s->base.pack_reclength+
1557
1541
                                     _my_calc_total_blob_length(info,record))))
1558
1542
        return(-1);
1559
1543
    }
1601
1585
  my_errno=0;
1602
1586
err:
1603
1587
  if (buffer != info->rec_buff)
1604
 
    my_afree((unsigned char*) buffer);
 
1588
    my_afree((uchar*) buffer);
1605
1589
  return(my_errno);
1606
1590
}
1607
1591
 
1608
1592
 
1609
1593
        /* Compare file to buffert */
1610
1594
 
1611
 
static int _mi_cmp_buffer(File file, const unsigned char *buff, my_off_t filepos,
1612
 
                          uint32_t length)
 
1595
static int _mi_cmp_buffer(File file, const uchar *buff, my_off_t filepos,
 
1596
                          uint length)
1613
1597
{
1614
 
  uint32_t next_length;
1615
 
  unsigned char temp_buff[IO_SIZE*2];
 
1598
  uint next_length;
 
1599
  uchar temp_buff[IO_SIZE*2];
1616
1600
 
1617
1601
  next_length= IO_SIZE*2 - (uint) (filepos & (IO_SIZE-1));
1618
1602
 
1668
1652
    != 0        Error
1669
1653
*/
1670
1654
 
1671
 
int _mi_read_rnd_dynamic_record(MI_INFO *info, unsigned char *buf,
 
1655
int _mi_read_rnd_dynamic_record(MI_INFO *info, uchar *buf,
1672
1656
                                register my_off_t filepos,
1673
 
                                bool skip_deleted_blocks)
 
1657
                                my_bool skip_deleted_blocks)
1674
1658
{
1675
1659
  int block_of_record, info_read, save_errno;
1676
 
  uint32_t left_len,b_type;
1677
 
  unsigned char *to= NULL;
 
1660
  uint left_len,b_type;
 
1661
  uchar *to= NULL;
1678
1662
  MI_BLOCK_INFO block_info;
1679
1663
  MYISAM_SHARE *share=info->s;
1680
1664
 
1709
1693
    }
1710
1694
    if (info->opt_flag & READ_CACHE_USED)
1711
1695
    {
1712
 
      if (_mi_read_cache(&info->rec_cache,(unsigned char*) block_info.header,filepos,
 
1696
      if (_mi_read_cache(&info->rec_cache,(uchar*) block_info.header,filepos,
1713
1697
                         sizeof(block_info.header),
1714
1698
                         (!block_of_record && skip_deleted_blocks ?
1715
1699
                          READING_NEXT : 0) | READING_HEADER))
1764
1748
 
1765
1749
    /* copy information that is already read */
1766
1750
    {
1767
 
      uint32_t offset=(uint) (block_info.filepos - filepos);
1768
 
      uint32_t tmp_length= (sizeof(block_info.header) - offset);
 
1751
      uint offset=(uint) (block_info.filepos - filepos);
 
1752
      uint tmp_length= (sizeof(block_info.header) - offset);
1769
1753
      filepos=block_info.filepos;
1770
1754
 
1771
1755
      if (tmp_length > block_info.data_len)
1772
1756
        tmp_length= block_info.data_len;
1773
1757
      if (tmp_length)
1774
1758
      {
1775
 
        memcpy(to, block_info.header+offset,tmp_length);
 
1759
        memcpy((uchar*) to, block_info.header+offset,tmp_length);
1776
1760
        block_info.data_len-=tmp_length;
1777
1761
        left_len-=tmp_length;
1778
1762
        to+=tmp_length;
1784
1768
    {
1785
1769
      if (info->opt_flag & READ_CACHE_USED)
1786
1770
      {
1787
 
        if (_mi_read_cache(&info->rec_cache,(unsigned char*) to,filepos,
 
1771
        if (_mi_read_cache(&info->rec_cache,(uchar*) to,filepos,
1788
1772
                           block_info.data_len,
1789
1773
                           (!block_of_record && skip_deleted_blocks) ?
1790
1774
                           READING_NEXT : 0))
1797
1781
            block_info.filepos + block_info.data_len &&
1798
1782
            flush_io_cache(&info->rec_cache))
1799
1783
          goto err;
1800
 
        /* my_seek(info->dfile,filepos,MY_SEEK_SET,MYF(0)); */
1801
 
        if (my_read(info->dfile,(unsigned char*) to,block_info.data_len,MYF(MY_NABP)))
 
1784
        /* VOID(my_seek(info->dfile,filepos,MY_SEEK_SET,MYF(0))); */
 
1785
        if (my_read(info->dfile,(uchar*) to,block_info.data_len,MYF(MY_NABP)))
1802
1786
        {
1803
1787
          if (my_errno == -1)
1804
1788
            my_errno= HA_ERR_WRONG_IN_RECORD;   /* Unexpected end of file */
1831
1815
  my_errno=HA_ERR_WRONG_IN_RECORD;              /* Something is fatal wrong */
1832
1816
err:
1833
1817
  save_errno=my_errno;
1834
 
  _mi_writeinfo(info,0);
 
1818
  VOID(_mi_writeinfo(info,0));
1835
1819
  return(my_errno=save_errno);
1836
1820
}
1837
1821
 
1838
1822
 
1839
1823
        /* Read and process header from a dynamic-record-file */
1840
1824
 
1841
 
uint32_t _mi_get_block_info(MI_BLOCK_INFO *info, File file, my_off_t filepos)
 
1825
uint _mi_get_block_info(MI_BLOCK_INFO *info, File file, my_off_t filepos)
1842
1826
{
1843
 
  uint32_t return_val=0;
1844
 
  unsigned char *header=info->header;
 
1827
  uint return_val=0;
 
1828
  uchar *header=info->header;
1845
1829
 
1846
1830
  if (file >= 0)
1847
1831
  {
1850
1834
      pointer set to the end of the header after this function.
1851
1835
      my_pread() may leave the file pointer untouched.
1852
1836
    */
1853
 
    my_seek(file,filepos,MY_SEEK_SET,MYF(0));
 
1837
    VOID(my_seek(file,filepos,MY_SEEK_SET,MYF(0)));
1854
1838
    if (my_read(file, header, sizeof(info->header),MYF(0)) !=
1855
1839
        sizeof(info->header))
1856
1840
      goto err;
1878
1862
    info->prev_filepos=mi_sizekorr(header+12);
1879
1863
#if SIZEOF_OFF_T == 4
1880
1864
    if ((mi_uint4korr(header+4) != 0 &&
1881
 
         (mi_uint4korr(header+4) != UINT32_MAX ||
1882
 
          info->next_filepos != UINT32_MAX) ||
 
1865
         (mi_uint4korr(header+4) != (ulong) ~0 ||
 
1866
          info->next_filepos != (ulong) ~0)) ||
1883
1867
        (mi_uint4korr(header+12) != 0 &&
1884
 
         (mi_uint4korr(header+12) != UINT32_MAX ||
1885
 
          info->prev_filepos != UINT32_MAX))
 
1868
         (mi_uint4korr(header+12) != (ulong) ~0 ||
 
1869
          info->prev_filepos != (ulong) ~0)))
1886
1870
      goto err;
1887
1871
#endif
1888
1872
    return return_val | BLOCK_DELETED;          /* Deleted block */